From a8e85f2c41bd9d0740459c46fd29375c5a92eb76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 16 Mar 2016 22:43:04 +0100 Subject: [PATCH] FetchedResultsController: cleanup --- GRDB/Record/FetchedResultsController.swift | 80 +++++++--------------- 1 file changed, 23 insertions(+), 57 deletions(-) diff --git a/GRDB/Record/FetchedResultsController.swift b/GRDB/Record/FetchedResultsController.swift index be8ee1839..563369e12 100644 --- a/GRDB/Record/FetchedResultsController.swift +++ b/GRDB/Record/FetchedResultsController.swift @@ -154,51 +154,40 @@ public class FetchedResultsController { d[0][col + 1] = changes } - guard m > 0 && n > 0 else { return d[m][n] } - - // Indexes into the two collections. - var sx: Array.Index - var tx = t.startIndex + if m == 0 || n == 0 { + // Pure deletions or insertions + return d[m][n] + } // Fill body of matrix. - - for j in 1...n { - sx = s.startIndex - - for i in 1...m { + for tx in 0.. { /// Returns an array where deletion/insertion pairs of the same element are replaced by `.Move` change. func standardizeChanges(changes: [ResultChange>]) -> [ResultChange>] { - /// Returns a potential .Move `ResultChange` based on an array of `ResultChange` elements and a `ResultChange` to match up against. - /// If `deletionOrInsertion` is a deletion or an insertion, and there is a matching inverse insertion/deletion with the same value in the array, a corresponding `.Move` update is returned. - /// As a convenience, the index of the matched `ResultChange` into `changes` is returned as well. - func mergedChangeFromChanges(changes: [ResultChange>], deletionOrInsertion change: ResultChange>) -> (mergedChange: ResultChange>, obsoleteIndex: Int)? { + /// Returns a potential .Move or .Update if *change* has a matching change in *changes*: + /// If *change* is a deletion or an insertion, and there is a matching inverse + /// insertion/deletion with the same value in *changes*, a corresponding .Move or .Update is returned. + /// As a convenience, the index of the matched change is returned as well. + func mergedChange(change: ResultChange>, inChanges changes: [ResultChange>]) -> (mergedChange: ResultChange>, obsoleteIndex: Int)? { let obsoleteIndex = changes.indexOf { earlierChange in return earlierChange.isMoveCounterpart(change, identityComparator: { (lhs, rhs) in return identityComparator(lhs.result, rhs.result) }) } @@ -252,7 +242,7 @@ public class FetchedResultsController { var mergedChanges: [ResultChange>] = [] var updateChanges: [ResultChange>] = [] for change in changes { - if let (mergedChange, obsoleteIndex) = mergedChangeFromChanges(mergedChanges, deletionOrInsertion: change) { + if let (mergedChange, obsoleteIndex) = mergedChange(change, inChanges: mergedChanges) { mergedChanges.removeAtIndex(obsoleteIndex) switch mergedChange { case .Update: @@ -268,30 +258,6 @@ public class FetchedResultsController { } return standardizeChanges(d[m][n]) - - // NSFetchResultsController sometimes add some extra notifications - // We'll see in the future if it is needed or not - //for (sIndex, sItem) in s.enumerate() { - // if let tIndex = t.indexOf(sItem) as? Int where tIndex != sIndex { - // var shouldAdd = true - // for edit in reducedAndOrdoredEdits { - // switch edit.operation { - // case .Move: - // if edit.value == sItem { - // shouldAdd = false - // } - // default: break - // } - // } - // - // if shouldAdd { - // // TODO! get value from destination array!!!! - // let move = Edit(.Move(origin: sIndex), value: sItem, destination: tIndex) - // print("Added extra \(move)") - // reducedAndOrdoredEdits.append(move) - // } - // } - //} } }