Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatabaseWriter.concurrentRead #409

Merged
merged 12 commits into from
Sep 13, 2018
Merged

DatabaseWriter.concurrentRead #409

merged 12 commits into from
Sep 13, 2018

Conversation

groue
Copy link
Owner

@groue groue commented Sep 9, 2018

This pull requests improves the DatabaseWriter protocol by deprecating the readFromCurrentState method, and introducing concurrentRead as a replacement.

protocol DatabaseWriter {
    func concurrentRead<T>(_ block: @escaping (Database) throws -> T) -> Future<T>
}

The problem with the deprecated readFromCurrentState is that it is a synchronous DatabaseQueue method, and an asynchronous DatabasePool method. This makes writing code that target both classes difficult.

concurrentRead, instead, returns a Future value that client code can wait until the concurrent fetch is completed.

concurrentRead aims at helping database observation tools like FetchedRecordsController and RxGRDB: it fetches values from a known database state (right after a transaction has been committed), without blocking the database writer dispatch queue longer than necessary. DatabasePool.concurrentRead, especially, only blocks until snapshot isolation has been established in a concurrent reader.

try writer.writeWithoutTransaction { db in
    // Delete all players
    try Player.deleteAll()
    
    // Count players concurrently
    let future = writer.concurrentRead { db in
        return try Player.fetchCount()
    }
    
    // Insert a player
    try Player(...).insert(db)
    
    // Guaranteed to be zero
    let count = try future.wait()
}

@groue groue merged commit 3bf3256 into development Sep 13, 2018
@groue groue deleted the feature/concurrentRead branch September 13, 2018 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant