From fdf33cbf815e5f5b6efdb024c51efd519fed410f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 17 Jan 2017 15:01:14 +0100 Subject: [PATCH] Experimental: FetchedRecordsController.allowBackgroundTracking --- GRDB/Record/FetchedRecordsController.swift | 49 ++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/GRDB/Record/FetchedRecordsController.swift b/GRDB/Record/FetchedRecordsController.swift index 30904f167..e48faf35f 100644 --- a/GRDB/Record/FetchedRecordsController.swift +++ b/GRDB/Record/FetchedRecordsController.swift @@ -1,3 +1,7 @@ +#if os(iOS) + import UIKit +#endif + /// You use FetchedRecordsController to track changes in the results of an /// SQLite request. /// @@ -208,9 +212,33 @@ public final class FetchedRecordsController { return } + var willProcessTransaction: () -> () = { } + var didProcessTransaction: () -> () = { } + #if os(iOS) + if let application = application { + var backgroundTaskID: UIBackgroundTaskIdentifier! = nil + willProcessTransaction = { + backgroundTaskID = application.beginBackgroundTask { + application.endBackgroundTask(backgroundTaskID) + } + } + didProcessTransaction = { + application.endBackgroundTask(backgroundTaskID) + } + } + #endif + let initialItems = fetchedItems databaseWriter.write { db in - let fetchAndNotifyChanges = makeFetchAndNotifyChangesFunction(controller: self, fetchAlongside: fetchAlongside, itemsAreIdentical: itemsAreIdentical, willChange: willChange, onChange: onChange, didChange: didChange) + let fetchAndNotifyChanges = makeFetchAndNotifyChangesFunction( + controller: self, + fetchAlongside: fetchAlongside, + itemsAreIdentical: itemsAreIdentical, + willProcessTransaction: willProcessTransaction, + willChange: willChange, + onChange: onChange, + didChange: didChange, + didProcessTransaction: didProcessTransaction) let observer = FetchedRecordsObserver(selectionInfo: request.selectionInfo, fetchAndNotifyChanges: fetchAndNotifyChanges) self.observer = observer if let initialItems = initialItems { @@ -237,6 +265,14 @@ public final class FetchedRecordsController { self.errorHandler = errorHandler } + #if os(iOS) + var application: UIApplication? + + /// TODO + public func allowBackgroundTracking(in application: UIApplication) { + self.application = application + } + #endif // MARK: - Accessing Records @@ -437,6 +473,7 @@ private final class FetchedRecordsObserver : Transaction fileprivate func makeFetchFunction( controller: FetchedRecordsController, fetchAlongside: @escaping (Database) throws -> T, + willProcessTransaction: @escaping () -> (), completion: @escaping (Result<(fetchedItems: [Item], fetchedAlongside: T, observer: FetchedRecordsObserver)>) -> () ) -> (FetchedRecordsObserver) -> () { @@ -454,6 +491,8 @@ fileprivate func makeFetchFunction( // Return if fetched records controller has been deallocated guard let request = controller?.request, let databaseWriter = controller?.databaseWriter else { return } + willProcessTransaction() + // Fetch items. // // This method is called from the database writer's serialized @@ -510,9 +549,11 @@ fileprivate func makeFetchAndNotifyChangesFunction( controller: FetchedRecordsController, fetchAlongside: @escaping (Database) throws -> T, itemsAreIdentical: @escaping ItemComparator, + willProcessTransaction: @escaping () -> (), willChange: ((FetchedRecordsController, _ fetchedAlongside: T) -> ())?, onChange: ((FetchedRecordsController, Record, FetchedRecordChange) -> ())?, - didChange: ((FetchedRecordsController, _ fetchedAlongside: T) -> ())? + didChange: ((FetchedRecordsController, _ fetchedAlongside: T) -> ())?, + didProcessTransaction: @escaping () -> () ) -> (FetchedRecordsObserver) -> () { // Make sure we keep a weak reference to the fetched records controller, @@ -522,7 +563,7 @@ fileprivate func makeFetchAndNotifyChangesFunction( // // Should controller become strong at any point before callbacks are // called, such unowned reference would have an opportunity to crash. - return makeFetchFunction(controller: controller, fetchAlongside: fetchAlongside) { [weak controller] result in + return makeFetchFunction(controller: controller, fetchAlongside: fetchAlongside, willProcessTransaction: willProcessTransaction) { [weak controller] result in // Return if fetched records controller has been deallocated guard let callbackQueue = controller?.queue else { return } @@ -532,6 +573,7 @@ fileprivate func makeFetchAndNotifyChangesFunction( // Now we can retain controller guard let strongController = controller else { return } strongController.errorHandler?(strongController, error) + didProcessTransaction() } case .success((fetchedItems: let fetchedItems, fetchedAlongside: let fetchedAlongside, observer: let observer)): @@ -567,6 +609,7 @@ fileprivate func makeFetchAndNotifyChangesFunction( } } didChange?(strongController, fetchedAlongside) + didProcessTransaction() } } }