diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift index 1dd14bd7a..767ae273e 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift @@ -87,16 +87,28 @@ class MasterViewController: UITableViewController, FetchedRecordsControllerDeleg // MARK: - FetchedRecordsControllerDelegate - func controllerWillChangeContent(controller: FetchedRecordsController) { - + func controllerWillUpdate(controller: FetchedRecordsController) { + tableView.beginUpdates() } - func controller(controller: FetchedRecordsController, didChangeRecord record: Person, atIndexPath indexPath: NSIndexPath?, forChangeType type: FetchedRecordsChangeType, newIndexPath: NSIndexPath?) { - + func controllerUpdate(controller: FetchedRecordsController, update: FetchedRecordsUpdate) { + switch update { + case .Insert(_, let at): + tableView.insertRowsAtIndexPaths([at], withRowAnimation: .Automatic) + + case .Delete(_, let from): + tableView.deleteRowsAtIndexPaths([from], withRowAnimation: .Automatic) + + case .Move(_, let from, let to): + tableView.moveRowAtIndexPath(from, toIndexPath: to) + + case .Reload(_, let at): + tableView.reloadRowsAtIndexPaths([at], withRowAnimation: .Automatic) + } } - func controllerDidChangeContent(controller: FetchedRecordsController) { - tableView.reloadData() + func controllerDidFinishUpdates(controller: FetchedRecordsController) { + tableView.endUpdates() } } diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Person.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Person.swift index d5bce9b25..c21750d52 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Person.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/Person.swift @@ -38,3 +38,14 @@ class Person : Record { super.init(row: row) } } + +extension Person : Hashable { + + var hashValue: Int { + return self.id.hashValue + } +} + +func ==(lhs: Person, rhs: Person) -> Bool { + return lhs.id == rhs.id +} diff --git a/GRDB.xcworkspace/contents.xcworkspacedata b/GRDB.xcworkspace/contents.xcworkspacedata index fc0fea041..4cfe076f5 100644 --- a/GRDB.xcworkspace/contents.xcworkspacedata +++ b/GRDB.xcworkspace/contents.xcworkspacedata @@ -4,6 +4,9 @@ + + diff --git a/GRDB/Record/FetchedRecordsController.swift b/GRDB/Record/FetchedRecordsController.swift index 889dc7bc2..eeb166adf 100644 --- a/GRDB/Record/FetchedRecordsController.swift +++ b/GRDB/Record/FetchedRecordsController.swift @@ -8,7 +8,7 @@ import UIKit -public class FetchedRecordsController { +public class FetchedRecordsController> { // MARK: - Initialization public required init(sql: String, databaseQueue: DatabaseQueue) { @@ -70,57 +70,163 @@ public class FetchedRecordsController { T.fetchAll(db, self.sql) } } + + func calculateUpdatesFrom(fromRecords: [T], toRecords: [T]) -> [FetchedRecordsUpdate] { + + var updates = [FetchedRecordsUpdate]() + var currentRecords = fromRecords + let finalIndexPathForItem: [T:NSIndexPath]! + var currentIndexPathForItem: [T:NSIndexPath]! + + func indexPaths(items: [T]) -> [T:NSIndexPath] { + var indexPathForItem = [T:NSIndexPath]() + for (index, item) in items.enumerate() { + let indexPath = NSIndexPath(forItem: index, inSection: 0) + indexPathForItem[item] = indexPath + } + return indexPathForItem + } + + func apply(update: FetchedRecordsUpdate) { + currentRecords.applyUpdate(update) + currentIndexPathForItem = indexPaths(currentRecords) + } + + finalIndexPathForItem = indexPaths(toRecords) + currentIndexPathForItem = indexPaths(currentRecords) + + // 2 - INSERTS + for item: T in toRecords { + if let _ = currentIndexPathForItem[item] { + // item was there + } else { + // Insert + guard let newIndexPath = finalIndexPathForItem[item] else { + print("WTF?!") + break + } + + let update = FetchedRecordsUpdate.Insert(item: item, at: newIndexPath) + updates.append(update) + apply(update) + } + } + + // 3 - DELETES, MOVES & RELOAD + for item: T in currentRecords { + guard let oldIndexPath = currentIndexPathForItem[item] else { + print("WTF?!") + break + } + if let newIndexPath = finalIndexPathForItem[item] { + + if oldIndexPath == newIndexPath { + // item updates ? + // let update = FetchedRecordsUpdate.Reload(item: item, at: newIndexPath) + // updates.append(update) + // apply(update) + } else { + // item moved + let update = FetchedRecordsUpdate.Move(item: item, from: oldIndexPath, to: newIndexPath) + updates.append(update) + apply(update) + } + + } else { + // item deleted + let update = FetchedRecordsUpdate.Delete(item: item, from: oldIndexPath) + updates.append(update) + apply(update) + } + } + + return updates + } } extension FetchedRecordsController : TransactionObserverType { - public func databaseDidChangeWithEvent(event: DatabaseEvent) { } - public func databaseWillCommit() throws { } - + public func databaseDidRollback(db: Database) { } public func databaseDidCommit(db: Database) { - let newRecords = T.fetchAll(db, self.sql) - print("newRecords => \(newRecords)") - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let oldRecords = self.fetchedRecords - print("oldRecords => \(oldRecords)") - // horrible diff computation + // horrible diff computation dispatch_async(dispatch_get_main_queue()) { - self.delegate?.controllerWillChangeContent(self) + self.delegate?.controllerWillUpdate(self) // after controllerWillChangeContent self.fetchedRecords = newRecords // notify horrible diff computation + for update in self.calculateUpdatesFrom(oldRecords!, toRecords: newRecords) { + self.delegate?.controllerUpdate(self, update: update) + } // done - self.delegate?.controllerDidChangeContent(self) + self.delegate?.controllerDidFinishUpdates(self) } } } - - public func databaseDidRollback(db: Database) { } } + public protocol FetchedRecordsControllerDelegate : class { - func controllerWillChangeContent(controller: FetchedRecordsController) - func controller(controller: FetchedRecordsController, didChangeRecord record: T, atIndexPath indexPath: NSIndexPath?, forChangeType type: FetchedRecordsChangeType, newIndexPath: NSIndexPath?) - func controllerDidChangeContent(controller: FetchedRecordsController) + func controllerWillUpdate(controller: FetchedRecordsController) + func controllerUpdate(controller: FetchedRecordsController, update: FetchedRecordsUpdate) + func controllerDidFinishUpdates(controller: FetchedRecordsController) } + public extension FetchedRecordsControllerDelegate { - func controllerWillChangeContent(controller: FetchedRecordsController) {} - func controller(controller: FetchedRecordsController, didChangeRecord record: T, atIndexPath indexPath: NSIndexPath?, forChangeType type: FetchedRecordsChangeType, newIndexPath: NSIndexPath?) {} - func controllerDidChangeContent(controller: FetchedRecordsController) {} + func controllerWillUpdate(controller: FetchedRecordsController) {} + func controllerUpdate(controller: FetchedRecordsController, update: FetchedRecordsUpdate) {} + func controllerDidFinishUpdates(controller: FetchedRecordsController) {} } -public enum FetchedRecordsChangeType : UInt { - case Insert - case Delete - case Move - case Update + +public enum FetchedRecordsUpdate { + case Insert(item:T, at: NSIndexPath) + case Delete(item:T, from: NSIndexPath) + case Move(item:T, from: NSIndexPath, to: NSIndexPath) + case Reload(item:T, at: NSIndexPath) + + var description: String { + switch self { + case .Insert(let item, let at): + return "\(item) inserted at \(at)" + + case .Delete(let item, let from): + return "\(item) deleted from \(from)" + + case .Move(let item, let from, let to): + return "\(item) moved from \(from) to \(to)" + + case .Reload(let item, let at): + return "\(item) updated at \(at)" + } + } } + +extension Array { + mutating func applyUpdate(update: FetchedRecordsUpdate) { + switch update { + case .Insert(let item, let at): + self.insert(item, atIndex: at.item) + + case .Delete(_, let from): + self.removeAtIndex(from.item) + + case .Move(let item, let from, let to): + self.removeAtIndex(from.item) + self.insert(item, atIndex: to.item) + + case .Reload(_, _): break + } + print(update.description) + } +} + diff --git a/Playgrounds/ArrayMutations.playground/Contents.swift b/Playgrounds/ArrayMutations.playground/Contents.swift new file mode 100644 index 000000000..bce881cb9 --- /dev/null +++ b/Playgrounds/ArrayMutations.playground/Contents.swift @@ -0,0 +1,129 @@ +//: Playground - noun: a place where people can play + +import UIKit + +let yesterdayArrivals = ["Thomas", "Sylvaine", "Pascal", "Pierlo", "Gwen", "Julien", "Alex"] +let todayArrivals = ["Guillaume", "Pascal", "Sylvaine", "Pierlo", "Gwen", "Alex", "Fabien", "Maxime", "Greg"] + +enum Update { + case Insert(item:T, at: NSIndexPath) + case Delete(item:T, from: NSIndexPath) + case Move(item:T, from: NSIndexPath, to: NSIndexPath) + case Reload(item:T, at: NSIndexPath) + + var description: String { + switch self { + case .Insert(let item, let at): + return "\(item) inserted at \(at)" + + case .Delete(let item, let from): + return "\(item) deleted from \(from)" + + case .Move(let item, let from, let to): + return "\(item) moved from \(from) to \(to)" + + case .Reload(let item, let at): + return "\(item) updated at \(at)" + } + } +} + +func calculateUpdatesFrom(from: [String], to: [String]) -> [Update] { + + var updates = [Update]() + var current = from + let finalIndexPathForItem: [String:NSIndexPath]! + var currentIndexPathForItem: [String:NSIndexPath]! + + func indexPaths(items: [String]) -> [String:NSIndexPath] { + var indexPathForItem = [String:NSIndexPath]() + for (index, item) in items.enumerate() { + let indexPath = NSIndexPath(forItem: index, inSection: 0) + indexPathForItem[item] = indexPath + } + return indexPathForItem + } + + func apply(update: Update) { + current.applyUpdate(update) + currentIndexPathForItem = indexPaths(current) + } + + finalIndexPathForItem = indexPaths(to) + currentIndexPathForItem = indexPaths(current) + + // 2 - INSERTS + for item: String in to { + if let _ = currentIndexPathForItem[item] { + // item was there + } else { + // Insert + let newIndexPath = finalIndexPathForItem[item]! + let update = Update.Insert(item: item, at: newIndexPath) + updates.append(update) + apply(update) + } + } + + // 3 - DELETED & MOVES + currentIndexPathForItem = indexPaths(current) + for item: String in from { + let oldIndexPath = currentIndexPathForItem[item]! + if let newIndexPath = finalIndexPathForItem[item] { + // item moved + let update = Update.Move(item: item, from: oldIndexPath, to: newIndexPath) + updates.append(update) + apply(update) + } else { + // item deleted + let update = Update.Delete(item: item, from: oldIndexPath) + updates.append(update) + apply(update) + } + } + +// // 4 - UPDATES +// for item: String in from { +// let oldIndexPath = currentIndexPathForItem[item]! +// if (treatedIndexPaths.contains(oldIndexPath)) { +// continue; +// } +// +// if let newIndexPath = finalIndexPathForItem[item] where newIndexPath == oldIndexPath { +// // item has not moved, just reload +// updates.append(Update.Reload(item: item, at: oldIndexPath)) +// } +// } + + return updates +} + +extension Array { + mutating func applyUpdate(update: Update) { + switch update { + case .Insert(let item, let at): + self.insert(item, atIndex: at.item) + + case .Delete(_, let from): + self.removeAtIndex(from.item) + + case .Move(let item, let from, let to): + self.removeAtIndex(from.item) + self.insert(item, atIndex: to.item) + + case .Reload(_, _): break + } + print(update.description) + } +} + + +var test = yesterdayArrivals +print(test) +let updates = calculateUpdatesFrom(yesterdayArrivals, to: todayArrivals) +for update: Update in updates { + print(update.description) + test.applyUpdate(update) +} +print(test) + diff --git a/Playgrounds/ArrayMutations.playground/contents.xcplayground b/Playgrounds/ArrayMutations.playground/contents.xcplayground new file mode 100644 index 000000000..5da2641c9 --- /dev/null +++ b/Playgrounds/ArrayMutations.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/ArrayMutations.playground/playground.xcworkspace/contents.xcworkspacedata b/Playgrounds/ArrayMutations.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 000000000..919434a62 --- /dev/null +++ b/Playgrounds/ArrayMutations.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Playgrounds/ArrayMutations.playground/timeline.xctimeline b/Playgrounds/ArrayMutations.playground/timeline.xctimeline new file mode 100644 index 000000000..92f7bccba --- /dev/null +++ b/Playgrounds/ArrayMutations.playground/timeline.xctimeline @@ -0,0 +1,17 @@ + + + + + + + + +