diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift index 90e8d5ea6..9f1b0f1fd 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/MasterViewController.swift @@ -1,14 +1,18 @@ import UIKit import GRDB -class MasterViewController: UITableViewController { +class MasterViewController: UITableViewController, FetchedRecordsControllerDelegate { var fetchedRecordsController: FetchedRecordsController! override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = self.editButtonItem() configureToolbar() - configureFetchedRecordsController() + + let fetchRequest = Person.filter(Col.visible).order(Col.position, Col.firstName, Col.lastName) + fetchedRecordsController = FetchedRecordsController(dbQueue, fetchRequest, compareRecordsByPrimaryKey: true) + fetchedRecordsController.delegate = self + fetchedRecordsController.performFetch() } override func viewWillAppear(animated: Bool) { @@ -76,6 +80,44 @@ class MasterViewController: UITableViewController { } + // MARK: - FetchedRecordsControllerDelegate + + func controllerWillChangeRecords(controller: FetchedRecordsController) { + tableView.beginUpdates() + } + + func controller(controller: FetchedRecordsController, didChangeRecord record: T, withEvent event:FetchedRecordsEvent) { + switch event { + case .Insertion(let indexPath): + tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) + + case .Deletion(let indexPath): + tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) + + case .Update(let indexPath, _): + if let cell = tableView.cellForRowAtIndexPath(indexPath) { + configureCell(cell, atIndexPath: indexPath) + } + + case .Move(let indexPath, let newIndexPath, _): +// // Technique 1 (replace) +// tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) +// tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade) + + // Technique 2 (move) + let cell = tableView.cellForRowAtIndexPath(indexPath) + tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath) + if let cell = cell { + configureCell(cell, atIndexPath: newIndexPath) + } + } + } + + func controllerDidChangeRecords(controller: FetchedRecordsController) { + tableView.endUpdates() + } + + // MARK: - Private func swapNames() { @@ -106,54 +148,6 @@ class MasterViewController: UITableViewController { ] self.navigationController?.toolbarHidden = false } - - private func configureFetchedRecordsController() { - // The fetched objects - let fetchRequest = Person.filter(Col.visible).order(Col.position, Col.firstName, Col.lastName) - fetchedRecordsController = FetchedRecordsController(dbQueue, fetchRequest, compareRecordsByPrimaryKey: true) - - fetchedRecordsController.willChange { [weak self] in - // Events are about to be applied - self?.tableView.beginUpdates() - } - - fetchedRecordsController.onEvent { [weak self] (person, event) in - guard let strongSelf = self else { return } - - // Apply individual event - switch event { - case .Insertion(let indexPath): - strongSelf.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) - - case .Deletion(let indexPath): - strongSelf.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) - - case .Update(let indexPath, _): - if let cell = strongSelf.tableView.cellForRowAtIndexPath(indexPath) { - strongSelf.configureCell(cell, atIndexPath: indexPath) - } - - case .Move(let indexPath, let newIndexPath, _): -// // Technique 1 (replace) -// strongSelf.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) -// strongSelf.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade) - - // Technique 2 (move) - let cell = strongSelf.tableView.cellForRowAtIndexPath(indexPath) - strongSelf.tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath) - if let cell = cell { - strongSelf.configureCell(cell, atIndexPath: newIndexPath) - } - } - } - - fetchedRecordsController.didChange { [weak self] in - // All events have been applied - self?.tableView.endUpdates() - } - - fetchedRecordsController.performFetch() - } } extension MutableCollectionType where Index == Int { diff --git a/GRDB/Record/FetchedRecordsController.swift b/GRDB/Record/FetchedRecordsController.swift index ff0833a9b..e9401186e 100644 --- a/GRDB/Record/FetchedRecordsController.swift +++ b/GRDB/Record/FetchedRecordsController.swift @@ -64,26 +64,13 @@ public final class FetchedRecordsController { // MARK: - Configuration + public weak var delegate: FetchedRecordsControllerDelegate? + // Configuration: database /// The databaseWriter public let database: DatabaseWriter - /// MUST BE CALLED ON mainQueue (TODO: say it nicely) - public func willChange(callback:() -> ()) { - willChangeCallback = callback - } - - /// MUST BE CALLED ON mainQueue (TODO: say it nicely) - public func didChange(callback:() -> ()) { - didChangeCallback = callback - } - - /// MUST BE CALLED ON mainQueue (TODO: say it nicely) - public func onEvent(callback:(record: T, event: FetchedRecordsEvent) -> ()) { - eventCallback = callback - } - // MARK: - Accessing records @@ -140,11 +127,6 @@ public final class FetchedRecordsController { // The items exposed on public API private var mainItems: [Item] = [] // protected by mainQueue - // The change callbacks - private var willChangeCallback: (() -> ())? // protected by mainQueue - private var didChangeCallback: (() -> ())? // protected by mainQueue - private var eventCallback: ((record: T, event: FetchedRecordsEvent) -> ())? // protected by mainQueue - // The record comparator. When T adopts MutablePersistable, we need to wait // for performFetch() in order to build it, because private var isSameRecord: ((T, T) -> Bool) = { _ in false } @@ -380,22 +362,48 @@ extension FetchedRecordsController : TransactionObserverType { guard let strongSelf = self else { return } - strongSelf.willChangeCallback?() + strongSelf.delegate?.controllerWillChangeRecords(strongSelf) strongSelf.mainItems = newItems - if let eventCallback = strongSelf.eventCallback { - for change in changes { - eventCallback(record: change.record, event: change.event) - } + for change in changes { + strongSelf.delegate?.controller(strongSelf, didChangeRecord: change.record, withEvent: change.event) } - strongSelf.didChangeCallback?() + strongSelf.delegate?.controllerDidChangeRecords(strongSelf) } } } } +// ============================================================================= +// MARK: - FetchedRecordsControllerDelegate + +/// TODO +public protocol FetchedRecordsControllerDelegate : class { + /// TODO: document that these are called on mainQueue + func controllerWillChangeRecords(controller: FetchedRecordsController) + + /// TODO: document that these are called on mainQueue + func controller(controller: FetchedRecordsController, didChangeRecord record: T, withEvent event:FetchedRecordsEvent) + + /// TODO: document that these are called on mainQueue + func controllerDidChangeRecords(controller: FetchedRecordsController) +} + +extension FetchedRecordsControllerDelegate { + /// TODO + func controllerWillChangeRecords(controller: FetchedRecordsController) { } + + /// TODO + func controller(controller: FetchedRecordsController, didChangeRecord record: T, withEvent event:FetchedRecordsEvent) { } + + /// TODO + func controllerDidChangeRecords(controller: FetchedRecordsController) { } +} + + + // ============================================================================= // MARK: - FetchedRecordsSectionInfo