mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Update FetchedRecordsController + make use of its update APIs
This commit is contained in:
1 parent
81c22fcbc2
commit
5ee6ccdf48
8 files changed
+319
-30
No files matched your search
@@ -87,16 +87,28 @@ class MasterViewController: UITableViewController, FetchedRecordsControllerDeleg
|
||||
|
||||
// MARK: - FetchedRecordsControllerDelegate
|
||||
|
||||
func controllerWillChangeContent<Person>(controller: FetchedRecordsController<Person>) {
|
||||
|
||||
func controllerWillUpdate<T>(controller: FetchedRecordsController<T>) {
|
||||
tableView.beginUpdates()
|
||||
}
|
||||
|
||||
func controller<Person>(controller: FetchedRecordsController<Person>, didChangeRecord record: Person, atIndexPath indexPath: NSIndexPath?, forChangeType type: FetchedRecordsChangeType, newIndexPath: NSIndexPath?) {
|
||||
|
||||
func controllerUpdate<T>(controller: FetchedRecordsController<T>, update: FetchedRecordsUpdate<T>) {
|
||||
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<Person>(controller: FetchedRecordsController<Person>) {
|
||||
tableView.reloadData()
|
||||
func controllerDidFinishUpdates<T>(controller: FetchedRecordsController<T>) {
|
||||
tableView.endUpdates()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
+3
@@ -4,6 +4,9 @@
|
||||
<Group
|
||||
location = "group:Playgrounds"
|
||||
name = "Playgrounds">
|
||||
<FileRef
|
||||
location = "group:ArrayMutations.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:JSONSynchronization.playground">
|
||||
</FileRef>
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
public class FetchedRecordsController<T: DatabaseTableMapping> {
|
||||
public class FetchedRecordsController<T: protocol<DatabaseTableMapping, Hashable>> {
|
||||
|
||||
// MARK: - Initialization
|
||||
public required init(sql: String, databaseQueue: DatabaseQueue) {
|
||||
@@ -70,57 +70,163 @@ public class FetchedRecordsController<T: DatabaseTableMapping> {
|
||||
T.fetchAll(db, self.sql)
|
||||
}
|
||||
}
|
||||
|
||||
func calculateUpdatesFrom(fromRecords: [T], toRecords: [T]) -> [FetchedRecordsUpdate<T>] {
|
||||
|
||||
var updates = [FetchedRecordsUpdate<T>]()
|
||||
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<T>) {
|
||||
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<T: DatabaseTableMapping>(controller: FetchedRecordsController<T>)
|
||||
func controller<T: DatabaseTableMapping>(controller: FetchedRecordsController<T>, didChangeRecord record: T, atIndexPath indexPath: NSIndexPath?, forChangeType type: FetchedRecordsChangeType, newIndexPath: NSIndexPath?)
|
||||
func controllerDidChangeContent<T: DatabaseTableMapping>(controller: FetchedRecordsController<T>)
|
||||
func controllerWillUpdate<T>(controller: FetchedRecordsController<T>)
|
||||
func controllerUpdate<T>(controller: FetchedRecordsController<T>, update: FetchedRecordsUpdate<T>)
|
||||
func controllerDidFinishUpdates<T>(controller: FetchedRecordsController<T>)
|
||||
}
|
||||
|
||||
|
||||
public extension FetchedRecordsControllerDelegate {
|
||||
func controllerWillChangeContent<T: DatabaseTableMapping>(controller: FetchedRecordsController<T>) {}
|
||||
func controller<T: DatabaseTableMapping>(controller: FetchedRecordsController<T>, didChangeRecord record: T, atIndexPath indexPath: NSIndexPath?, forChangeType type: FetchedRecordsChangeType, newIndexPath: NSIndexPath?) {}
|
||||
func controllerDidChangeContent<T: DatabaseTableMapping>(controller: FetchedRecordsController<T>) {}
|
||||
func controllerWillUpdate<T>(controller: FetchedRecordsController<T>) {}
|
||||
func controllerUpdate<T>(controller: FetchedRecordsController<T>, update: FetchedRecordsUpdate<T>) {}
|
||||
func controllerDidFinishUpdates<T>(controller: FetchedRecordsController<T>) {}
|
||||
}
|
||||
|
||||
public enum FetchedRecordsChangeType : UInt {
|
||||
case Insert
|
||||
case Delete
|
||||
case Move
|
||||
case Update
|
||||
|
||||
public enum FetchedRecordsUpdate<T> {
|
||||
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<Array.Generator.Element>) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T> {
|
||||
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<String>] {
|
||||
|
||||
var updates = [Update<String>]()
|
||||
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<String>) {
|
||||
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<Array.Generator.Element>) {
|
||||
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)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='ios'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:">
|
||||
</FileRef>
|
||||
</Workspace>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
<LoggerValueHistoryTimelineItem
|
||||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=1800&EndingColumnNumber=32&EndingLineNumber=55&StartingColumnNumber=9&StartingLineNumber=55&Timestamp=471525999.04912"
|
||||
lockedSize = "{587, 155}"
|
||||
selectedRepresentationIndex = "0"
|
||||
shouldTrackSuperviewWidth = "NO">
|
||||
</LoggerValueHistoryTimelineItem>
|
||||
<LoggerValueHistoryTimelineItem
|
||||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=2232&EndingColumnNumber=26&EndingLineNumber=68&StartingColumnNumber=9&StartingLineNumber=68&Timestamp=471525999.049235"
|
||||
selectedRepresentationIndex = "0"
|
||||
shouldTrackSuperviewWidth = "NO">
|
||||
</LoggerValueHistoryTimelineItem>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
||||
Reference in new issue
Block a user