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

GRDB 7: Async database accesses honor Task cancellation #1610

Merged
merged 8 commits into from
Sep 1, 2024

Conversation

groue
Copy link
Owner

@groue groue commented Sep 1, 2024

In GRDB 6, asynchronous database accesses such as try await read { ... } or try await write { ... } complete even if the wrapper Task is cancelled.

In GRDB 7, asynchronous database accesses respect Task cancellation. If a Task is cancelled, reads and writes throw a CancellationError, pending transactions are rolled back, and the database is not modified. The only SQL statement that can execute in a cancelled database access is ROLLBACK.

The effect of this change on your application depends on how it uses tasks. For example, take care of database jobs initiated frop the task SwiftUI modifier.

If you want an asynchronous database access to always complete, regardless of Task cancellation, wrap it in an unstructured Task:

// Create a new Task in order to ignore
// cancellation of the current task, and
// make sure database changes are always
// committed to disk.
let task = Task {
    try await writer.write { ... }
}
// If needed, wait for the database job to complete:
try await task.value

Other asynchronous database accesses, such as methods accepting a completion blocks (asyncRead, etc.), Combine publishers, RxSwift observables, do not handle cancellation and will proceed to completion by default.

Semaphores allow tests to precisely schedule ordering of async jobs
@groue groue force-pushed the dev/GRDB7-cancellable-database-access branch from 4b4f94a to 1ec7762 Compare September 1, 2024 13:24
@groue groue force-pushed the dev/GRDB7-cancellable-database-access branch from 1ec7762 to 71f5554 Compare September 1, 2024 13:35
@groue groue merged commit a02601c into GRDB7 Sep 1, 2024
8 checks passed
@groue groue deleted the dev/GRDB7-cancellable-database-access branch September 1, 2024 14:30
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