From c5cfd0762bdc8198b8f67c1ff9238b7b512cba72 Mon Sep 17 00:00:00 2001 From: Denys Telezhkin Date: Thu, 1 Feb 2018 13:49:39 +0200 Subject: [PATCH] fix a bug, that prevented datasource from being updated when used in context of performWithoutAnimations method of MemoryStorage. --- CHANGELOG.md | 2 ++ .../MemoryStorage+UpdateWithoutAnimations.swift | 14 +++++++++++++- .../MemoryStorage/MemoryStorageSearchSpec.swift | 4 +++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91f0465f..79bd0124 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. # Next +* Fixed a bug, that prevented datasource from being updated when `updateWithoutAnimations` method on `MemoryStorage` was used. + ## [7.0.0](https://github.com/DenHeadless/DTModelStorage/releases/tag/7.0.0) ## [7.0.0-beta.1](https://github.com/DenHeadless/DTModelStorage/releases/tag/7.0.0-beta.1) diff --git a/Source/Core/MemoryStorage+UpdateWithoutAnimations.swift b/Source/Core/MemoryStorage+UpdateWithoutAnimations.swift index 477fef7c..f7215753 100644 --- a/Source/Core/MemoryStorage+UpdateWithoutAnimations.swift +++ b/Source/Core/MemoryStorage+UpdateWithoutAnimations.swift @@ -32,8 +32,20 @@ extension MemoryStorage open func updateWithoutAnimations(_ block: () -> Void) { let delegate = self.delegate - self.delegate = nil + let recordingDelegate = RecordingDelegate() + self.delegate = recordingDelegate block() + recordingDelegate.update?.applyDeferredDatasourceUpdates() self.delegate = delegate } } + +private class RecordingDelegate: StorageUpdating { + var update : StorageUpdate? + + func storageDidPerformUpdate(_ update: StorageUpdate) { + self.update = update + } + + func storageNeedsReloading() {} +} diff --git a/Tests/Specs/MemoryStorage/MemoryStorageSearchSpec.swift b/Tests/Specs/MemoryStorage/MemoryStorageSearchSpec.swift index 3e36a6c3..ac67dfab 100644 --- a/Tests/Specs/MemoryStorage/MemoryStorageSearchSpec.swift +++ b/Tests/Specs/MemoryStorage/MemoryStorageSearchSpec.swift @@ -92,9 +92,11 @@ class MemoryStorageSearchSpec: XCTestCase { } func testUpdateWithoutAnimations() { + storage.defersDatasourceUpdates = true storage.updateWithoutAnimations { - expect(self.storage.delegate).to(beNil()) + storage.addItems([1,2]) } + expect(self.storage.items(inSection: 0)?.flatMap { $0 as? Int} ?? []) == [1,2] } func testEmptySection()