diff --git a/GRDB/Record/FetchedRecordsController.swift b/GRDB/Record/FetchedRecordsController.swift index 66ca8027f..7b623ca44 100644 --- a/GRDB/Record/FetchedRecordsController.swift +++ b/GRDB/Record/FetchedRecordsController.swift @@ -11,31 +11,34 @@ public class FetchedRecordsController { // MARK: - Initialization - public convenience init(_ database: DatabaseWriter, _ sql: String, arguments: StatementArguments? = nil, isSameRecord: ((T, T) -> Bool)? = nil) { + // TODO: document that queue MUST be serial + public convenience init(_ database: DatabaseWriter, _ sql: String, arguments: StatementArguments? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), isSameRecord: ((T, T) -> Bool)? = nil) { let source: Source = .SQL(sql, arguments) - self.init(database: database, source: source, isSameRecord: isSameRecord) + self.init(database: database, source: source, queue: queue, isSameRecord: isSameRecord) } + // TODO: document that queue MUST be serial public convenience init(_ database: DatabaseWriter, _ request: FetchRequest, isSameRecord: ((T, T) -> Bool)? = nil) { let source: Source = .FetchRequest(request) self.init(database: database, source: source, isSameRecord: isSameRecord) } - private convenience init(database: DatabaseWriter, source: Source, isSameRecord: ((T, T) -> Bool)?) { + private convenience init(database: DatabaseWriter, source: Source, queue: dispatch_queue_t = dispatch_get_main_queue(), isSameRecord: ((T, T) -> Bool)?) { let isSameRecordBuilder: (Database) -> (T, T) -> Bool if let isSameRecord = isSameRecord { isSameRecordBuilder = { _ in isSameRecord } } else { isSameRecordBuilder = { _ in { _ in return false } } } - self.init(database: database, source: source, isSameRecordBuilder: isSameRecordBuilder) + self.init(database: database, source: source, queue: queue, isSameRecordBuilder: isSameRecordBuilder) } - private init(database: DatabaseWriter, source: Source, isSameRecordBuilder: (Database) -> (T, T) -> Bool) { + private init(database: DatabaseWriter, source: Source, queue: dispatch_queue_t, isSameRecordBuilder: (Database) -> (T, T) -> Bool) { self.source = source self.database = database self.isSameRecordBuilder = isSameRecordBuilder self.diffQueue = dispatch_queue_create("GRDB.FetchedRecordsController.diff", nil) + self.mainQueue = queue database.addTransactionObserver(self) } @@ -74,7 +77,8 @@ public class FetchedRecordsController { private var needsComputeChanges = false // protected by database writer queue /// Dedicated to fetchedRecords, recordAtIndexPath - private var mainItems: [Item]? // protected by main thread + private var mainItems: [Item]? // protected by mainQueue + public private(set) var mainQueue: dispatch_queue_t /// Dedicated to change computation private var diffItems: [Item]? // protected by diffQueue @@ -83,9 +87,8 @@ public class FetchedRecordsController { // MARK: - Accessing records + /// MUST BE CALLED ON mainQueue (TODO: say it nicely) 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 @@ -101,8 +104,9 @@ public class FetchedRecordsController { /// Returns the records of the query. /// Returns nil if the performQuery: hasn't been called. + /// + /// MUST BE CALLED ON mainQueue (TODO: say it nicely) public var fetchedRecords: [T]? { - precondition(NSThread.isMainThread(), "Must be called on the main thread") if let mainItems = self.mainItems { return mainItems.map { $0.record } } @@ -111,9 +115,10 @@ public class FetchedRecordsController { /// Returns the fetched record at a given indexPath. + /// + /// MUST BE CALLED ON mainQueue (TODO: say it nicely) public func recordAtIndexPath(indexPath: NSIndexPath) -> T { - precondition(NSThread.isMainThread(), "Must be called on the main thread") - return self.mainItems![indexPath.indexAtPosition(1)].record + return mainItems![indexPath.indexAtPosition(1)].record } /// Returns the indexPath of a given record. @@ -256,14 +261,17 @@ public class FetchedRecordsController { } extension FetchedRecordsController where T: MutablePersistable { - public convenience init(_ database: DatabaseWriter, _ sql: String, arguments: StatementArguments? = nil, compareRecordsByPrimaryKey: Bool) { + + // TODO: document that queue MUST be serial + public convenience init(_ database: DatabaseWriter, _ sql: String, arguments: StatementArguments? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), compareRecordsByPrimaryKey: Bool) { let source: Source = .SQL(sql, arguments) - self.init(database: database, source: source, isSameRecordBuilder: { db in try! T.primaryKeyComparator(db) }) + self.init(database: database, source: source, queue: queue, isSameRecordBuilder: { db in try! T.primaryKeyComparator(db) }) } - - public convenience init(_ database: DatabaseWriter, _ request: FetchRequest, compareRecordsByPrimaryKey: Bool) { + + // TODO: document that queue MUST be serial + public convenience init(_ database: DatabaseWriter, _ request: FetchRequest, queue: dispatch_queue_t = dispatch_get_main_queue(), compareRecordsByPrimaryKey: Bool) { let source: Source = .FetchRequest(request) - self.init(database: database, source: source, isSameRecordBuilder: { db in try! T.primaryKeyComparator(db) }) + self.init(database: database, source: source, queue: queue, isSameRecordBuilder: { db in try! T.primaryKeyComparator(db) }) } } @@ -312,7 +320,7 @@ extension FetchedRecordsController : TransactionObserverType { return } - dispatch_async(dispatch_get_main_queue()) { [weak self] in + dispatch_async(self.mainQueue) { [weak self] in // This code, submitted to the serial main queue, is guaranteed // to process the last database transaction: diff --git a/TODO.md b/TODO.md index 44b595c10..c04954d6f 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ -- [ ] FetchedRecordsController: let user choose its usage queue (it does not have to be the main queue) - [ ] DOC: Write an app like http://www.appcoda.com/swiftydb/ ? - [ ] DOC: Since commit e6010e334abdf98eb9f62c1d6abbb2a9e8cd7d19, one can not use the raw SQLite API without importing the SQLite module for the platform. We need to document that. - [ ] Query builder