FetchedRecordsController: fix concurrency bug

This commit is contained in:
Gwendal Roué committed 2016-03-29 08:53:55 +02:00
1 parent ab3ec443d2
commit efdc6053ff
2 files changed
+53 -32

No files matched your search

+3 -3
View File
@@ -348,16 +348,16 @@ extension DatabasePool : DatabaseWriter {
public func readFromWrite(block: (db: Database) -> Void) {
writer.preconditionValidQueue()
let s = dispatch_semaphore_create(0)
let semaphore = dispatch_semaphore_create(0)
self.readerPool.get { reader in
reader.performAsync { db in
db.read { db in
// Now we're isolated: release the writing queue
dispatch_semaphore_signal(s)
dispatch_semaphore_signal(semaphore)
block(db: db)
}
}
}
dispatch_semaphore_wait(s, DISPATCH_TIME_FOREVER)
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
}
}
+50 -29
View File
@@ -574,50 +574,71 @@ private final class FetchedRecordsObserver<Record: RowConvertible> : Transaction
// Invalidated?
guard let controller = self.controller else { return }
// Execute the fetch request.
// Fetch items.
//
// This method is called from the database writer's serialized queue, so
// that we can execute the fetch request before other writes have the
// opportunity to modify the database.
// that we can fetch items before other writes have the opportunity to
// modify the database.
//
// However, we don't have to block the writer queue for all the duration
// of the fetch. We just need to block the writer queue until we can
// perform a fetch in isolation. This is the role of the readFromWrite
// method:
// method (see below).
//
// However, our fetch will last for an unknown duration. And since we
// release the writer queue early, the next database modification will
// triggers this callback while our fetch is, maybe, still running. This
// next callback will also perform its own fetch, that will maybe end
// before our own fetch.
//
// We have to make sure that our fetch is processed *before* the next
// fetch: let's immediately dispatch the processing task in our
// serialized FIFO queue, but have it wait for our fetch to complete
// with a semaphore:
let semaphore = dispatch_semaphore_create(0)
var fetchedItems: [Item<Record>]! = nil
controller.writer.readFromWrite { db in
let statement = try! controller.source.selectStatement(db)
fetchedItems = Item<Record>.fetchAll(statement)
// Fetch is complete:
dispatch_semaphore_signal(semaphore)
}
// Process the fetched items
dispatch_async(queue) {
// Wait for the fetch to complete:
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
assert(fetchedItems != nil)
// Invalidated?
guard let controller = self.controller else { return }
let statement = try! controller.source.selectStatement(db)
let fetchedItems = Item<Record>.fetchAll(statement)
// Compute changes
let changes = self.computeChanges(from: self.items, to: fetchedItems)
self.items = fetchedItems
dispatch_async(self.queue) {
// No changes?
guard !changes.isEmpty else { return }
dispatch_async(controller.queue) {
// Invalidated?
guard let controller = self.controller else { return }
// Compute changes
let changes = self.computeChanges(from: self.items, to: fetchedItems)
self.items = fetchedItems
// No changes?
guard !changes.isEmpty else { return }
dispatch_async(controller.queue) {
// Invalidated?
guard let controller = self.controller else { return }
if let delegate = controller.delegate {
delegate.controllerWillChangeRecords(controller)
controller.fetchedItems = fetchedItems
for change in changes {
delegate.controller(controller, didChangeRecord: change.record, withEvent: change.event)
}
delegate.controllerDidChangeRecords(controller)
} else {
// There is no delegate interested in changes: stop tracking
// changes.
controller.stopTrackingChanges()
if let delegate = controller.delegate {
delegate.controllerWillChangeRecords(controller)
controller.fetchedItems = fetchedItems
for change in changes {
delegate.controller(controller, didChangeRecord: change.record, withEvent: change.event)
}
delegate.controllerDidChangeRecords(controller)
} else {
// There is no delegate interested in changes: stop tracking
// changes.
controller.stopTrackingChanges()
}
}
}