mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Updated documentation for #160
This commit is contained in:
1 parent
c5f3aa21b3
commit
de3d075ff2
4 files changed
+78
-48
No files matched your search
@@ -9,6 +9,42 @@ Release Notes
|
||||
- [FetchedRecordsController](https://github.com/groue/GRDB.swift#fetchedrecordscontroller) now outputs detailed change events on all platforms, and for both table and collection views. Merged [#160](https://github.com/groue/GRDB.swift/pull/160) by [@kdubb](https://github.com/kdubb).
|
||||
- Upgrade custom SQLite builds to [v3.16.2](http://www.sqlite.org/changes.html) (thanks to [@swiftlyfalling](https://github.com/swiftlyfalling/SQLiteLib)).
|
||||
|
||||
**Breaking Changes**
|
||||
|
||||
- It is now a programmer error to invoke `FetchedRecordsController.fetchedRecords` before `performFetch()`:
|
||||
|
||||
```diff
|
||||
final class FetchedRecordsController<Record: RowConvertible> {
|
||||
- var fetchedRecords: [Record]?
|
||||
+ var fetchedRecords: [Record]
|
||||
}
|
||||
```
|
||||
|
||||
- The fetched record type of a FetchedRecordsController is now infered from the request that feeds the controller:
|
||||
|
||||
```diff
|
||||
final class FetchedRecordsController<Record: RowConvertible> {
|
||||
- convenience init(...:request: Request) throws
|
||||
+ convenience init<Request>(...:request: Request) throws where Request: TypedRequest, Request.Fetched == Record
|
||||
}
|
||||
```
|
||||
|
||||
- FetchedRecordsController now automatically compares records by primary key when the record type adopts the TableMapping protocol, such as all Record subclasses. This feature used to require an explicit `compareRecordsByPrimaryKey` initialization parameter:
|
||||
|
||||
```diff
|
||||
extension FetchedRecordsController where Record: TableMapping {
|
||||
- convenience init(...:compareRecordsByPrimaryKey: Bool) throws
|
||||
}
|
||||
```
|
||||
|
||||
- Change tracking APIs have been modified:
|
||||
|
||||
```diff
|
||||
final class FetchedRecordsController<Record: RowConvertible> {
|
||||
- func trackChanges(recordsWillChange:tableViewEvent:recordsDidChange:)
|
||||
+ func trackChanges(willChange:onChange:didChange:)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## 0.99.2
|
||||
|
||||
@@ -4188,13 +4188,11 @@ protocol TransactionObserverType : class {
|
||||
|
||||
**You use FetchedRecordsController to track changes in the results of an SQLite request.**
|
||||
|
||||
**On iOS, FetchedRecordsController can feed a UITableView, and animate rows when the results of the request change.**
|
||||
**FetchedRecordsController can also feed table views, collection views, and animate cells when the results of the request change.**
|
||||
|
||||
It looks and behaves very much like [Core Data's NSFetchedResultsController](https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/).
|
||||
|
||||
Given a fetch request, and a type that adopts the [RowConvertible](#rowconvertible-protocol) protocol, such as a subclass of the [Record](#record-class) class, a FetchedRecordsController is able to track changes in the results of the fetch request, and notify of those changes.
|
||||
|
||||
On iOS, FetchedRecordsController is able to return the results of the request in a form that is suitable for a UITableView, with one table view row per fetched record.
|
||||
Given a fetch request, and a type that adopts the [RowConvertible](#rowconvertible-protocol) protocol, such as a subclass of the [Record](#record-class) class, a FetchedRecordsController is able to track changes in the results of the fetch request, notify of those changes, and return the results of the request in a form that is suitable for a table view or a collection view, with one cell per fetched record.
|
||||
|
||||
See [GRDBDemoiOS](DemoApps/GRDBDemoiOS) for an sample app that uses FetchedRecordsController.
|
||||
|
||||
@@ -4202,7 +4200,7 @@ See [GRDBDemoiOS](DemoApps/GRDBDemoiOS) for an sample app that uses FetchedRecor
|
||||
- [Responding to Changes](#responding-to-changes)
|
||||
- [The Changes Notifications](#the-changes-notifications)
|
||||
- [Modifying the Fetch Request](#modifying-the-fetch-request)
|
||||
- [FetchedRecordsController on iOS](#fetchedrecordscontroller-on-ios)
|
||||
- [Table and Collection Views](#table-and-collection-views)
|
||||
- [Implementing the Table View Datasource Methods](#implementing-the-table-view-datasource methods)
|
||||
- [Implementing Table View Updates](#implementing-table-view-updates)
|
||||
- [FetchedRecordsController Concurrency](#fetchedrecordscontroller-concurrency)
|
||||
@@ -4221,7 +4219,7 @@ class Person : Record { ... }
|
||||
let dbQueue = DatabaseQueue(...) // Or DatabasePool
|
||||
|
||||
// Using a Request from the Query Interface:
|
||||
let controller = FetchedRecordsController<Person>(
|
||||
let controller = FetchedRecordsController(
|
||||
dbQueue,
|
||||
request: Person.order(Column("name")))
|
||||
|
||||
@@ -4284,24 +4282,24 @@ let controller = try FetchedRecordsController(...)
|
||||
|
||||
controller.trackChanges(
|
||||
// controller's records are about to change:
|
||||
recordsWillChange: { controller in ... },
|
||||
willChange: { controller in ... },
|
||||
|
||||
// (iOS only) notification of individual record changes:
|
||||
tableViewEvent: { (controller, record, event) in ... },
|
||||
// notification of individual record changes:
|
||||
onChange: { (controller, record, change) in ... },
|
||||
|
||||
// controller's records have changed:
|
||||
recordsDidChange: { controller in ... })
|
||||
didChange: { controller in ... })
|
||||
|
||||
try controller.performFetch()
|
||||
```
|
||||
|
||||
See [Implementing Table View Updates](#implementing-table-view-updates) for more detail on table view updates on iOS.
|
||||
See [Implementing Table View Updates](#implementing-table-view-updates) for more detail on table view updates.
|
||||
|
||||
**All callbacks are optional.** When you only need to grab the latest results, you can omit the `recordsDidChange` argument name:
|
||||
**All callbacks are optional.** When you only need to grab the latest results, you can omit the `didChange` argument name:
|
||||
|
||||
```swift
|
||||
controller.trackChanges { controller in
|
||||
let newPersons = controller.fetchedRecords! // [Person]
|
||||
let newPersons = controller.fetchedRecords // [Person]
|
||||
}
|
||||
```
|
||||
|
||||
@@ -4310,12 +4308,12 @@ Callbacks have the fetched record controller itself as an argument: use it in or
|
||||
```swift
|
||||
// BAD: memory leak
|
||||
controller.trackChanges { _ in
|
||||
let newPersons = controller.fetchedRecords!
|
||||
let newPersons = controller.fetchedRecords
|
||||
}
|
||||
|
||||
// GOOD
|
||||
controller.trackChanges { controller in
|
||||
let newPersons = controller.fetchedRecords!
|
||||
let newPersons = controller.fetchedRecords
|
||||
}
|
||||
```
|
||||
|
||||
@@ -4331,9 +4329,9 @@ controller.trackChanges(
|
||||
// Fetch any extra value, for example the number of fetched records:
|
||||
return try Person.fetchCount(db)
|
||||
},
|
||||
recordsDidChange: { (controller, count) in
|
||||
didChange: { (controller, count) in
|
||||
// The extra value is the second argument.
|
||||
let recordsCount = controller.fetchedRecords!.count
|
||||
let recordsCount = controller.fetchedRecords.count
|
||||
assert(count == recordsCount) // guaranteed
|
||||
})
|
||||
```
|
||||
@@ -4370,25 +4368,23 @@ controller.setRequest(Person.order(Column("name")))
|
||||
try controller.performFetch()
|
||||
```
|
||||
|
||||
### FetchedRecordsController on iOS
|
||||
### Table and Collection Views
|
||||
|
||||
On iOS, FetchedRecordsController provides extra APIs that help feeding table views, and keeping them up-to-date with the database content.
|
||||
FetchedRecordsController let you feed table and collection views, and keep them up-to-date with the database content.
|
||||
|
||||
This require an extra step in the controller initialization, so that it can *identify* records.
|
||||
|
||||
Records are usually identified by their primary key. When the record type adopts the [TableMapping](#tablemapping-protocol) protocol, such as [Record](#record-class) subclasses, you will generally use the `compareRecordsByPrimaryKey` initialization argument:
|
||||
For nice animated updates, a fetched records controller needs to recognize identical records between two different result sets. When records adopt the [TableMapping](#tablemapping-protocol) protocol, they are automatically compared according to their primary key:
|
||||
|
||||
```swift
|
||||
let controller = FetchedRecordsController<Person>(
|
||||
class Person : TableMapping { ... }
|
||||
let controller = FetchedRecordsController(
|
||||
dbQueue,
|
||||
request: ...,
|
||||
compareRecordsByPrimaryKey: true)
|
||||
request: Person.all())
|
||||
```
|
||||
|
||||
When the record type does not adopt the [TableMapping](#tablemapping-protocol) protocol, be explicit:
|
||||
For other types, the fetched records controller needs you to be more explicit:
|
||||
|
||||
```swift
|
||||
let controller = FetchedRecordsController<Person>(
|
||||
let controller = FetchedRecordsController(
|
||||
dbQueue,
|
||||
request: ...,
|
||||
isSameRecord: { (person1, person2) in person1.id == person2.id })
|
||||
@@ -4434,7 +4430,7 @@ Yet, FetchedRecordsController can notify that the controller’s fetched records
|
||||
|
||||
##### Typical Table View Updates
|
||||
|
||||
For animated table view updates, use the `recordsWillChange` and `recordsDidChange` callbacks to bracket events provided by the fetched records controller, as illustrated in the following example:
|
||||
For animated table view updates, use the `willChange` and `didChange` callbacks to bracket events provided by the fetched records controller, as illustrated in the following example:
|
||||
|
||||
```swift
|
||||
// Assume self has a tableView property, and a cell configuration
|
||||
@@ -4442,13 +4438,13 @@ For animated table view updates, use the `recordsWillChange` and `recordsDidChan
|
||||
|
||||
controller.trackChanges(
|
||||
// controller's records are about to change:
|
||||
recordsWillChange: { [unowned self] _ in
|
||||
willChange: { [unowned self] _ in
|
||||
self.tableView.beginUpdates()
|
||||
},
|
||||
|
||||
// notification of individual record changes:
|
||||
tableViewEvent: { [unowned self] (controller, record, event) in
|
||||
switch event {
|
||||
onChange: { [unowned self] (controller, record, change) in
|
||||
switch change {
|
||||
case .insertion(let indexPath):
|
||||
self.tableView.insertRows(at: [indexPath], with: .fade)
|
||||
|
||||
@@ -4474,7 +4470,7 @@ controller.trackChanges(
|
||||
},
|
||||
|
||||
// controller's records have changed:
|
||||
recordsDidChange: { [unowned self] _ in
|
||||
didChange: { [unowned self] _ in
|
||||
self.tableView.endUpdates()
|
||||
})
|
||||
```
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
- [ ] v0.100.0
|
||||
- [ ] Update FetchedRecordsController documentation for https://github.com/groue/GRDB.swift/pull/160
|
||||
- [X] Update FetchedRecordsController documentation for https://github.com/groue/GRDB.swift/pull/160
|
||||
- [X] Use TypedRequest in FetchedRecordsController
|
||||
- [X] Remove compareRecordsByPrimaryKey initializer option
|
||||
- [X] Simplify FetchedRecordsController callbacks API
|
||||
|
||||
@@ -33,7 +33,7 @@ private class ChangesRecorder<Record: RowConvertible> {
|
||||
/// The default implementation does nothing.
|
||||
func controller(_ controller: FetchedRecordsController<Record>, didChangeRecord record: Record, with change: FetchedRecordChange) {
|
||||
if recordsOnFirstEvent == nil {
|
||||
recordsOnFirstEvent = controller.fetchedRecords!
|
||||
recordsOnFirstEvent = controller.fetchedRecords
|
||||
}
|
||||
changes.append((record: record, change: change))
|
||||
}
|
||||
@@ -131,8 +131,8 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
|
||||
let controller = try FetchedRecordsController<Book>(dbQueue, sql: "SELECT * FROM books WHERE authorID = ?", arguments: [authorId])
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 1)
|
||||
XCTAssertEqual(controller.fetchedRecords![0].title, "Don Quixote")
|
||||
XCTAssertEqual(controller.fetchedRecords.count, 1)
|
||||
XCTAssertEqual(controller.fetchedRecords[0].title, "Don Quixote")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,8 +152,8 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
let adapter = ColumnMapping(["id": "_id", "authorId": "_authorId", "title": "_title"])
|
||||
let controller = try FetchedRecordsController<Book>(dbQueue, sql: "SELECT id AS _id, authorId AS _authorId, title AS _title FROM books WHERE authorID = ?", arguments: [authorId], adapter: adapter)
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 1)
|
||||
XCTAssertEqual(controller.fetchedRecords![0].title, "Don Quixote")
|
||||
XCTAssertEqual(controller.fetchedRecords.count, 1)
|
||||
XCTAssertEqual(controller.fetchedRecords[0].title, "Don Quixote")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,13 +168,13 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
let request = Person.order(Column("name"))
|
||||
let controller = try FetchedRecordsController(dbQueue, request: request)
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 2)
|
||||
XCTAssertEqual(controller.fetchedRecords![0].name, "Cervantes")
|
||||
XCTAssertEqual(controller.fetchedRecords![1].name, "Plato")
|
||||
XCTAssertEqual(controller.fetchedRecords.count, 2)
|
||||
XCTAssertEqual(controller.fetchedRecords[0].name, "Cervantes")
|
||||
XCTAssertEqual(controller.fetchedRecords[1].name, "Plato")
|
||||
}
|
||||
}
|
||||
|
||||
func testRecordsAreNotLoadedUntilPerformFetch() {
|
||||
func testSections() {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let arthur = Person(name: "Arthur")
|
||||
@@ -184,14 +184,13 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
|
||||
let request = Person.all()
|
||||
let controller = try FetchedRecordsController(dbQueue, request: request)
|
||||
XCTAssertTrue(controller.fetchedRecords == nil)
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.sections.count, 1)
|
||||
XCTAssertEqual(controller.sections[0].numberOfRecords, 1)
|
||||
XCTAssertEqual(controller.sections[0].records.count, 1)
|
||||
XCTAssertEqual(controller.sections[0].records[0].name, "Arthur")
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 1)
|
||||
XCTAssertEqual(controller.fetchedRecords![0].name, "Arthur")
|
||||
XCTAssertEqual(controller.fetchedRecords.count, 1)
|
||||
XCTAssertEqual(controller.fetchedRecords[0].name, "Arthur")
|
||||
XCTAssertEqual(controller.record(at: IndexPath(indexes: [0, 0])).name, "Arthur")
|
||||
XCTAssertEqual(controller.indexPath(for: arthur), IndexPath(indexes: [0, 0]))
|
||||
}
|
||||
@@ -202,9 +201,8 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let request = Person.all()
|
||||
let controller = try FetchedRecordsController(dbQueue, request: request)
|
||||
XCTAssertTrue(controller.fetchedRecords == nil)
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 0)
|
||||
XCTAssertEqual(controller.fetchedRecords.count, 0)
|
||||
|
||||
// Just like NSFetchedResultsController
|
||||
XCTAssertEqual(controller.sections.count, 1)
|
||||
@@ -814,7 +812,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
|
||||
let expectation = self.expectation(description: "expectation")
|
||||
controller.trackChanges {
|
||||
persons = $0.fetchedRecords!
|
||||
persons = $0.fetchedRecords
|
||||
expectation.fulfill()
|
||||
}
|
||||
try dbQueue.inTransaction { db in
|
||||
|
||||
Reference in new issue
Block a user