Skip to content

Commit 30ab534

Browse files
Merge pull request #55 from terenzeyuen/fix-move-conflicts
Fix move conflicts
2 parents 2748e87 + 7c6a972 commit 30ab534

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

Sourcing/FetchedResultsDataProvider.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ open class FetchedResultsDataProvider<Object: NSFetchRequestResult>: NSObject, N
4545
configure(fetchedResultsController)
4646

4747
try fetchedResultsController.performFetch()
48-
dataProviderDidChangeContets(with: nil)
48+
dataProviderDidChangeContents(with: nil)
4949
}
5050

5151
public func object(at indexPath: IndexPath) -> Object {
@@ -102,18 +102,31 @@ open class FetchedResultsDataProvider<Object: NSFetchRequestResult>: NSObject, N
102102
}
103103

104104
public func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
105-
dataProviderDidChangeContets(with: updates)
106-
let updatesByMoves = updates.map({ (operation: DataProviderUpdate<Object>) -> DataProviderUpdate<Object>? in
105+
let updatesIndexPaths = updates.flatMap { update -> IndexPath? in
106+
switch update {
107+
case .update(let indexPath, _):
108+
return indexPath
109+
default: return nil
110+
}
111+
}
112+
updates = updates.flatMap { update -> DataProviderUpdate<Object>? in
113+
if case .move(_, let newIndexPath) = update, updatesIndexPaths.contains(newIndexPath) {
114+
return nil
115+
}
116+
return update
117+
}
118+
dataProviderDidChangeContents(with: updates)
119+
let updatesByMoves = updates.flatMap { operation -> DataProviderUpdate<Object>? in
107120
if case .move(_, let newIndexPath) = operation {
108121
return .update(newIndexPath, object(at: newIndexPath))
109122
}
110123
return nil
111-
}).flatMap { $0 }
112-
dataProviderDidChangeContets(with: updatesByMoves)
124+
}
125+
dataProviderDidChangeContents(with: updatesByMoves)
113126
}
114127

115-
func dataProviderDidChangeContets(with updates: [DataProviderUpdate<Object>]?, triggerdByTableView: Bool = false) {
116-
if !triggerdByTableView {
128+
func dataProviderDidChangeContents(with updates: [DataProviderUpdate<Object>]? = nil, triggeredByTableView: Bool = false) {
129+
if !triggeredByTableView {
117130
whenDataProviderChanged?(updates)
118131
}
119132
dataProviderDidUpdate?(updates)

SourcingTests/FetchedResultsDataProviderTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ class FetchedResultsDataProviderTests: XCTestCase {
198198
}
199199
}
200200

201+
func testConflictIndexPathsForMoveUpdate() {
202+
//Given
203+
let updateIndexPath = IndexPath(row: 1, section: 0)
204+
let oldIndexPath = IndexPath(row: 0, section: 0)
205+
let newIndexPath = IndexPath(row: 1, section: 0)
206+
207+
//When
208+
dataProvider.controller(fetchedResultsController as! NSFetchedResultsController<NSFetchRequestResult>,
209+
didChange: 1, at: oldIndexPath, for: .move, newIndexPath: newIndexPath)
210+
dataProvider.controller(fetchedResultsController as! NSFetchedResultsController<NSFetchRequestResult>,
211+
didChange: 1, at: updateIndexPath, for: .update, newIndexPath: nil)
212+
dataProvider.controllerDidChangeContent(fetchedResultsController as! NSFetchedResultsController<NSFetchRequestResult>)
213+
214+
//Then
215+
XCTAssertEqual(dataProvider.updates.count, 1)
216+
if case .move = dataProvider.updates.first! {
217+
XCTFail()
218+
}
219+
}
220+
201221
func testProcessUpdates() {
202222
//Given
203223
var didUpdateNotification = false

0 commit comments

Comments
 (0)