FetchedRecordsController: add initializer that compares record by primary key (if the record adopts MutablePersistable)

This commit is contained in:
Gwendal Roué committed 2016-03-23 08:54:02 +01:00
1 parent 83c5afdd2d
commit d0c5e98366
2 files changed
+46 -26

No files matched your search

@@ -23,9 +23,7 @@ class MasterViewController: UITableViewController {
// The fetched objects
let fetchRequest = Person.filter(Col.visible).order(Col.position, Col.firstName, Col.lastName)
fetchedRecordsController = FetchedRecordsController(dbQueue, fetchRequest, isSameRecord: { (person1, person2) in
person1.id == person2.id
})
fetchedRecordsController = FetchedRecordsController(dbQueue, fetchRequest, compareRecordsByPrimaryKey: true)
fetchedRecordsController.willChange { [unowned self] in
// Events are about to be applied
+45 -23
View File
@@ -10,6 +10,7 @@ import UIKit
public class FetchedRecordsController<T: RowConvertible> {
// MARK: - Initialization
public convenience init(_ database: DatabaseWriter, _ sql: String, arguments: StatementArguments? = nil, isSameRecord: ((T, T) -> Bool)? = nil) {
let source: Source<T> = .SQL(sql, arguments)
self.init(database: database, source: source, isSameRecord: isSameRecord)
@@ -20,31 +21,22 @@ public class FetchedRecordsController<T: RowConvertible> {
self.init(database: database, source: source, isSameRecord: isSameRecord)
}
private init(database: DatabaseWriter, source: Source<T>, isSameRecord: ((T, T) -> Bool)?) {
self.source = source
self.database = database
private convenience init(database: DatabaseWriter, source: Source<T>, isSameRecord: ((T, T) -> Bool)?) {
let isSameRecordBuilder: (Database) -> (T, T) -> Bool
if let isSameRecord = isSameRecord {
self.isSameRecord = isSameRecord
isSameRecordBuilder = { _ in isSameRecord }
} else {
self.isSameRecord = { _ in return false }
isSameRecordBuilder = { _ in { _ in return false } }
}
self.diffQueue = dispatch_queue_create("GRDB.FetchedRecordsController.diff", nil)
database.addTransactionObserver(self)
self.init(database: database, source: source, isSameRecordBuilder: isSameRecordBuilder)
}
public func performFetch() {
precondition(NSThread.isMainThread(), "Must be called on the main thread")
// Use database.write, so that we are serialized with transaction
// callbacks, which happen on the writing queue.
try! database.write { db in
let statement = try self.source.selectStatement(db)
self.observedTables = statement.sourceTables
self.mainItems = Item<T>.fetchAll(statement)
}
dispatch_sync(self.diffQueue) {
self.diffItems = self.mainItems
}
private init(database: DatabaseWriter, source: Source<T>, isSameRecordBuilder: (Database) -> (T, T) -> Bool) {
self.source = source
self.database = database
self.isSameRecordBuilder = isSameRecordBuilder
self.diffQueue = dispatch_queue_create("GRDB.FetchedRecordsController.diff", nil)
database.addTransactionObserver(self)
}
@@ -68,7 +60,8 @@ public class FetchedRecordsController<T: RowConvertible> {
}
private var eventCallback: ((record: T, event: FetchedRecordsEvent) -> ())?
private let isSameRecord: (T, T) -> Bool
private var isSameRecord: ((T, T) -> Bool)?
private let isSameRecordBuilder: (Database) -> (T, T) -> Bool
/// The source
@@ -89,6 +82,22 @@ public class FetchedRecordsController<T: RowConvertible> {
// MARK: - Accessing records
public func performFetch() {
precondition(NSThread.isMainThread(), "Must be called on the main thread")
// Use database.write, so that we are serialized with transaction
// callbacks, which happen on the writing queue.
database.write { db in
let statement = try! self.source.selectStatement(db)
self.observedTables = statement.sourceTables
self.mainItems = Item<T>.fetchAll(statement)
self.isSameRecord = self.isSameRecordBuilder(db)
}
dispatch_sync(self.diffQueue) {
self.diffItems = self.mainItems
}
}
/// Returns the records of the query.
/// Returns nil if the performQuery: hasn't been called.
@@ -249,6 +258,19 @@ public class FetchedRecordsController<T: RowConvertible> {
}
}
extension FetchedRecordsController where T: MutablePersistable {
public convenience init(_ database: DatabaseWriter, _ sql: String, arguments: StatementArguments? = nil, compareRecordsByPrimaryKey: Bool) {
let source: Source<T> = .SQL(sql, arguments)
self.init(database: database, source: source, isSameRecordBuilder: { db in try! T.primaryKeyComparator(db) })
}
public convenience init(_ database: DatabaseWriter, _ request: FetchRequest<T>, compareRecordsByPrimaryKey: Bool) {
let source: Source<T> = .FetchRequest(request)
self.init(database: database, source: source, isSameRecordBuilder: { db in try! T.primaryKeyComparator(db) })
}
}
// MARK: - <TransactionObserverType>
extension FetchedRecordsController : TransactionObserverType {
@@ -273,7 +295,7 @@ extension FetchedRecordsController : TransactionObserverType {
}
needsComputeChanges = false
guard diffItems != nil else {
guard let diffItems = diffItems, let isSameRecord = isSameRecord else {
// performFetch() has not been called yet
return
}
@@ -286,7 +308,7 @@ extension FetchedRecordsController : TransactionObserverType {
// to process the last database transaction:
// Read/write diffItems in self.diffQueue
let changes = FetchedRecordsController.computeChanges(fromRows: self.diffItems!, toRows: newItems, isSameRecord: self.isSameRecord)
let changes = FetchedRecordsController.computeChanges(fromRows: diffItems, toRows: newItems, isSameRecord: isSameRecord)
self.diffItems = newItems
guard !changes.isEmpty else {