FetchedRecordsController: Back to delegate pattern

This commit is contained in:
Gwendal Roué committed 2016-03-24 13:35:11 +01:00
1 parent eba839b58e
commit b13ad57ea5
2 files changed
+78 -76

No files matched your search

@@ -1,14 +1,18 @@
import UIKit
import GRDB
class MasterViewController: UITableViewController {
class MasterViewController: UITableViewController, FetchedRecordsControllerDelegate {
var fetchedRecordsController: FetchedRecordsController<Person>!
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<T>(controller: FetchedRecordsController<T>) {
tableView.beginUpdates()
}
func controller<T>(controller: FetchedRecordsController<T>, 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<T>(controller: FetchedRecordsController<T>) {
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 {
+34 -26
View File
@@ -64,26 +64,13 @@ public final class FetchedRecordsController<T: RowConvertible> {
// 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<T: RowConvertible> {
// The items exposed on public API
private var mainItems: [Item<T>] = [] // 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<T>(controller: FetchedRecordsController<T>)
/// TODO: document that these are called on mainQueue
func controller<T>(controller: FetchedRecordsController<T>, didChangeRecord record: T, withEvent event:FetchedRecordsEvent)
/// TODO: document that these are called on mainQueue
func controllerDidChangeRecords<T>(controller: FetchedRecordsController<T>)
}
extension FetchedRecordsControllerDelegate {
/// TODO
func controllerWillChangeRecords<T>(controller: FetchedRecordsController<T>) { }
/// TODO
func controller<T>(controller: FetchedRecordsController<T>, didChangeRecord record: T, withEvent event:FetchedRecordsEvent) { }
/// TODO
func controllerDidChangeRecords<T>(controller: FetchedRecordsController<T>) { }
}
// =============================================================================
// MARK: - FetchedRecordsSectionInfo