mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
#147: FetchedRecordsController.performFetch() can now throw database errors; remove work around for SR-2623
This commit is contained in:
1 parent
8d9d0dd826
commit
268080cbc5
10 files changed
+42
-54
No files matched your search
@@ -21,6 +21,7 @@ Release Notes
|
||||
- `Persistable.exists(_:)` can now throw database errors.
|
||||
- `DatabasePool.read(_:)` `DatabasePool.nonIsolatedRead(_:)` can now throw database errors.
|
||||
- `fetchAll` and `fetchOne` can now throw database errors.
|
||||
- `FetchedRecordsController.performFetch()` can now throw database errors.
|
||||
- The `fetch` method have been removed, along with `DatabaseSequence` and `DatabaseIterator`.
|
||||
|
||||
```swift
|
||||
|
||||
@@ -8,8 +8,8 @@ class InterfaceController: WKInterfaceController {
|
||||
override func awake(withContext context: Any?) {
|
||||
super.awake(withContext: context)
|
||||
|
||||
let persons = dbQueue.inDatabase { db in
|
||||
Person.order(Column("name")).fetchAll(db)
|
||||
let persons = try! dbQueue.inDatabase { db in
|
||||
try Person.order(Column("name")).fetchAll(db)
|
||||
}
|
||||
|
||||
table.setNumberOfRows(persons.count, withRowType: "Person")
|
||||
|
||||
@@ -47,7 +47,7 @@ class PersonsViewController: UITableViewController {
|
||||
recordsDidChange: { [unowned self] _ in
|
||||
self.tableView.endUpdates()
|
||||
})
|
||||
personsController.performFetch()
|
||||
try! personsController.performFetch()
|
||||
|
||||
configureToolbar()
|
||||
}
|
||||
@@ -176,7 +176,7 @@ extension PersonsViewController {
|
||||
setEditing(false, animated: true)
|
||||
|
||||
try! dbQueue.inTransaction { db in
|
||||
for person in Person.fetch(db) {
|
||||
for person in try Person.fetchAll(db) {
|
||||
person.score = Person.randomScore()
|
||||
try person.update(db)
|
||||
}
|
||||
@@ -190,7 +190,7 @@ extension PersonsViewController {
|
||||
for _ in 0..<50 {
|
||||
DispatchQueue.global().async {
|
||||
try! dbQueue.inTransaction { db in
|
||||
if Person.fetchCount(db) == 0 {
|
||||
if try Person.fetchCount(db) == 0 {
|
||||
// Insert persons
|
||||
for _ in 0..<8 {
|
||||
try Person(name: Person.randomName(), score: Person.randomScore()).insert(db)
|
||||
@@ -203,12 +203,12 @@ extension PersonsViewController {
|
||||
}
|
||||
// Delete a person
|
||||
if arc4random_uniform(2) == 0 {
|
||||
if let person = Person.order(sql: "RANDOM()").fetchOne(db) {
|
||||
if let person = try Person.order(sql: "RANDOM()").fetchOne(db) {
|
||||
try person.delete(db)
|
||||
}
|
||||
}
|
||||
// Update some persons
|
||||
for person in Person.fetchAll(db) {
|
||||
for person in try Person.fetchAll(db) {
|
||||
if arc4random_uniform(2) == 0 {
|
||||
person.score = Person.randomScore()
|
||||
try person.update(db)
|
||||
|
||||
@@ -20,8 +20,8 @@ class SimplePersonsViewController: UITableViewController {
|
||||
}
|
||||
|
||||
private func loadPersons() {
|
||||
persons = dbQueue.inDatabase { db in
|
||||
Person.order(Column("score").desc, Column("name")).fetchAll(db)
|
||||
persons = try! dbQueue.inDatabase { db in
|
||||
try Person.order(Column("score").desc, Column("name")).fetchAll(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,15 +81,3 @@ extension DatabaseWriter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension DatabaseWriter {
|
||||
// Addresses https://github.com/groue/GRDB.swift/issues/117 and
|
||||
// https://bugs.swift.org/browse/SR-2623
|
||||
//
|
||||
// This method allows avoiding calling the regular rethrowing `write(_:)`
|
||||
// method from a property of type DatabaseWriter, and crashing the Swift 3
|
||||
// compiler of Xcode 8 GM Version 8.0 (8A218a)
|
||||
func writeForIssue117(_ block: (Database) -> Void) -> Void {
|
||||
write(block)
|
||||
}
|
||||
}
|
||||
@@ -133,7 +133,7 @@ public final class FetchedRecordsController<Record: RowConvertible> {
|
||||
///
|
||||
/// After executing this method, you can access the the fetched objects with
|
||||
/// the property fetchedRecords.
|
||||
public func performFetch() {
|
||||
public func performFetch() throws {
|
||||
// If some changes are currently processed, make sure they are
|
||||
// discarded. But preserve eventual changes processing for future
|
||||
// changes.
|
||||
@@ -144,10 +144,10 @@ public final class FetchedRecordsController<Record: RowConvertible> {
|
||||
// Fetch items on the writing dispatch queue, so that the transaction
|
||||
// observer is added on the same serialized queue as transaction
|
||||
// callbacks.
|
||||
databaseWriter.writeForIssue117 { db in
|
||||
try databaseWriter.write { db in
|
||||
// TODO: handle errors
|
||||
let (statement, adapter) = try! request.prepare(db)
|
||||
let initialItems = try! Array(Item<Record>.fetchCursor(statement, adapter: adapter))
|
||||
let (statement, adapter) = try request.prepare(db)
|
||||
let initialItems = try Array(Item<Record>.fetchCursor(statement, adapter: adapter))
|
||||
fetchedItems = initialItems
|
||||
|
||||
#if os(iOS)
|
||||
@@ -293,7 +293,7 @@ public final class FetchedRecordsController<Record: RowConvertible> {
|
||||
}
|
||||
|
||||
let initialItems = fetchedItems
|
||||
databaseWriter.writeForIssue117 { db in
|
||||
databaseWriter.write { db in
|
||||
if identicalItems == nil {
|
||||
identicalItems = identicalItemsFactory(db)
|
||||
}
|
||||
@@ -338,7 +338,7 @@ public final class FetchedRecordsController<Record: RowConvertible> {
|
||||
}
|
||||
|
||||
let initialItems = fetchedItems
|
||||
databaseWriter.writeForIssue117 { db in
|
||||
databaseWriter.write { db in
|
||||
let fetchAndNotifyChanges = makeFetchAndNotifyChangesFunction(controller: self, fetchAlongside: fetchAlongside, recordsWillChange: recordsWillChange, recordsDidChange: recordsDidChange)
|
||||
|
||||
let (statement, _) = try! request.prepare(db)
|
||||
@@ -402,7 +402,7 @@ public final class FetchedRecordsController<Record: RowConvertible> {
|
||||
// Replace observer so that it tracks a new set of columns,
|
||||
// and notify eventual changes
|
||||
let initialItems = fetchedItems
|
||||
databaseWriter.writeForIssue117 { db in
|
||||
databaseWriter.write { db in
|
||||
let (statement, _) = try! request.prepare(db)
|
||||
let observer = FetchedRecordsObserver(readInfo: statement.readInfo, fetchAndNotifyChanges: fetchAndNotifyChanges)
|
||||
self.observer = observer
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
- we'll have to deal with two input sources for dealing with transactions: statement compilation observation for savepoint statements, and transaction hooks for transaction statements. Tricky.
|
||||
- [ ] Attach databases (this could be the support for fetched records controller caches). Interesting question: what happens when one attaches a non-WAL db to a databasePool?
|
||||
- [ ] sqlite3_rekey is discouraged (https://github.com/ccgus/fmdb/issues/547#issuecomment-259219320)
|
||||
- [ ] Remove DatabaseWriter.writeForIssue117 when https://bugs.swift.org/browse/SR-2623 is fixed (remove `writeForIssue117`, use `write` instead, and build in Release configuration)
|
||||
- [ ] Restore dispatching tests in GRDBOSXTests (they are disabled in order to avoid linker errors)
|
||||
- DatabasePoolReleaseMemoryTests
|
||||
- DatabasePoolSchemaCacheTests
|
||||
|
||||
@@ -118,7 +118,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
let controller = FetchedRecordsController<Book>(dbQueue, sql: "SELECT * FROM books WHERE authorID = ?", arguments: [authorId])
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 1)
|
||||
XCTAssertEqual(controller.fetchedRecords![0].title, "Don Quixote")
|
||||
}
|
||||
@@ -139,7 +139,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
|
||||
let adapter = ColumnMapping(["id": "_id", "authorId": "_authorId", "title": "_title"])
|
||||
let controller = FetchedRecordsController<Book>(dbQueue, sql: "SELECT id AS _id, authorId AS _authorId, title AS _title FROM books WHERE authorID = ?", arguments: [authorId], adapter: adapter)
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 1)
|
||||
XCTAssertEqual(controller.fetchedRecords![0].title, "Don Quixote")
|
||||
}
|
||||
@@ -155,7 +155,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
|
||||
let request = Person.order(Column("name"))
|
||||
let controller = FetchedRecordsController<Person>(dbQueue, request: request)
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 2)
|
||||
XCTAssertEqual(controller.fetchedRecords![0].name, "Cervantes")
|
||||
XCTAssertEqual(controller.fetchedRecords![1].name, "Plato")
|
||||
@@ -173,7 +173,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
let request = Person.all()
|
||||
let controller = FetchedRecordsController<Person>(dbQueue, request: request)
|
||||
XCTAssertTrue(controller.fetchedRecords == nil)
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 1)
|
||||
XCTAssertEqual(controller.fetchedRecords![0].name, "Arthur")
|
||||
}
|
||||
@@ -193,7 +193,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
controller.trackChanges(
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// First insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -233,7 +233,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
controller.trackChanges(
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -291,7 +291,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
controller.trackChanges(
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -339,7 +339,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
controller.trackChanges(
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -382,7 +382,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
controller.trackChanges(
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -430,7 +430,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
controller.trackChanges(
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -503,7 +503,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let controller = FetchedRecordsController<Person>(dbQueue, request: Person.order(Column("name")))
|
||||
let recorder = ChangesRecorder<Person>()
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -530,7 +530,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let controller = FetchedRecordsController<Person>(dbQueue, request: Person.order(Column("name")))
|
||||
var persons: [Person] = []
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
let expectation = self.expectation(description: "expectation")
|
||||
controller.trackChanges {
|
||||
@@ -556,7 +556,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
fetchAlongside: { db in try Person.fetchCount(db) },
|
||||
recordsWillChange: { (controller, count) in recorder.controllerWillChange(controller, count: count) },
|
||||
recordsDidChange: { (controller, count) in recorder.controllerDidChange(controller, count: count) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// First insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
|
||||
@@ -103,7 +103,7 @@
|
||||
let request = Person.all()
|
||||
let controller = FetchedRecordsController<Person>(dbQueue, request: request, compareRecordsByPrimaryKey: true)
|
||||
XCTAssertTrue(controller.fetchedRecords == nil)
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.sections.count, 1)
|
||||
XCTAssertEqual(controller.sections[0].numberOfRecords, 1)
|
||||
XCTAssertEqual(controller.sections[0].records.count, 1)
|
||||
@@ -121,7 +121,7 @@
|
||||
let request = Person.all()
|
||||
let controller = FetchedRecordsController<Person>(dbQueue, request: request)
|
||||
XCTAssertTrue(controller.fetchedRecords == nil)
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 0)
|
||||
|
||||
// Just like NSFetchedResultsController
|
||||
@@ -144,7 +144,7 @@
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
tableViewEvent: { (controller, record, event) in recorder.controller(controller, didChangeRecord: record, withEvent: event) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// First insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -204,7 +204,7 @@
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
tableViewEvent: { (controller, record, event) in recorder.controller(controller, didChangeRecord: record, withEvent: event) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -283,7 +283,7 @@
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
tableViewEvent: { (controller, record, event) in recorder.controller(controller, didChangeRecord: record, withEvent: event) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -350,7 +350,7 @@
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
tableViewEvent: { (controller, record, event) in recorder.controller(controller, didChangeRecord: record, withEvent: event) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -406,7 +406,7 @@
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
tableViewEvent: { (controller, record, event) in recorder.controller(controller, didChangeRecord: record, withEvent: event) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -454,7 +454,7 @@
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
tableViewEvent: { (controller, record, event) in recorder.controller(controller, didChangeRecord: record, withEvent: event) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
enum EventTest {
|
||||
case I(String, Int) // insert string at index
|
||||
@@ -587,7 +587,7 @@
|
||||
recordsWillChange: { recorder.controllerWillChange($0) },
|
||||
tableViewEvent: { (controller, record, event) in recorder.controller(controller, didChangeRecord: record, withEvent: event) },
|
||||
recordsDidChange: { recorder.controllerDidChange($0) })
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
recorder.transactionExpectation = expectation(description: "expectation")
|
||||
@@ -658,7 +658,7 @@
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let controller = FetchedRecordsController<Person>(dbQueue, request: Person.order(Column("name")), compareRecordsByPrimaryKey: true)
|
||||
let recorder = ChangesRecorder<Person>()
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
// Insert
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -695,7 +695,7 @@
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let controller = FetchedRecordsController<Person>(dbQueue, request: Person.order(Column("name")), compareRecordsByPrimaryKey: true)
|
||||
var persons: [Person] = []
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
|
||||
let expectation = self.expectation(description: "expectation")
|
||||
controller.trackChanges {
|
||||
|
||||
@@ -682,7 +682,7 @@ class PrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
expectation.fulfill()
|
||||
}
|
||||
#endif
|
||||
controller.performFetch()
|
||||
try controller.performFetch()
|
||||
try dbQueue.inDatabase { db in
|
||||
person.name = "Barbara"
|
||||
try person.update(db)
|
||||
|
||||
Reference in new issue
Block a user