Skip to content

Conversation

@groue
Copy link
Owner

@groue groue commented Oct 17, 2021

This pull request brings support for Swift concurrency and async/await.

It implements the "Phase 1", as discussed in #1065, which means that the new async apis wrap the underlying DispatchQueue-based implementation.

// Async read
let playerCount = try await dbQueue.read { db in
    try Player.fetchCount(db)
}

// Async write
let newPlayerCount = try await dbQueue.write { db -> Int in
    try Player(...).insert(db)
    return try Player.fetchCount(db)
}

// Async erase, vacuum...
try await dbQueue.erase()
try await dbQueue.vacuum()

// Async migrate
try await migrator.migrate(dbQueue)

// Async database observation
let playerCounts = ValueObservation
    .tracking(Player.fetchCount)
    .values(in: dbQueue)
for try await playerCount in playerCounts { ... }

All async apis are 🔥 EXPERIMENTAL.

📦 Also included in this PR

🚧 Remaining tasks

  • Write more documentation
  • More tests

💝 WELCOMED CONTRIBUTIONS

  • I do not personally own a macOS computer able to run async/await. If you do, can you please check that the new apis are readily available, and report your findings? Thank you in advance. Monterey + M1 work fine
  • Please play with the new apis, and report any bug or ergonomics issue .
  • Please ask for features. The goal is that the async/await edition of GRDB ships with a maximum of batteries included, and all ideas are welcome.
  • Bring your experience about https://forums.swift.org/t/52862
  • Help fixing SR-15150
  • Help on the remaining tasks listed in the PR.

Come discuss, or submit pull requests against the dev/async branch!

cc @Nekitosss, @StuClift

groue added 26 commits October 17, 2021 13:23
write, writeWithoutTransaction, barrierWriteWithoutTransaction, erase, vacuum, vacuum(into:)
migrate(_:)
migrate(_:upTo:)
Also use .trackingConstantRegion since it triggers optimizations
This fixes the build for macOS 11.5
This file is designed to be copied into user apps: make sure the license comes along.
@groue groue changed the base branch from master to development October 17, 2021 13:27
groue added 2 commits October 17, 2021 21:25
See https://forums.swift.org/t/52862 for a longer explanation.

Fortunately, we can base @query on Combine publishers, which do not have this problem.
# Conflicts:
#	Documentation/DemoApps/GRDBDemoiOS/README.md
#	Documentation/DemoApps/README.md
Xcode 13.2 RC won't compile unless we make a Sendable copy of the row that could not be decoded.
@groue
Copy link
Owner Author

groue commented Dec 8, 2021

Xcode 13.2 RC is out, so we're close to a release!

groue added 13 commits December 8, 2021 08:00
Xcode 12 won't accept those flags. And we only need them starting Xcode 13.2
I believe this is incorrect: FB9801372 (UIApplication.didReceiveMemoryWarningNotification should not be declared @mainactor)

Meanwhile, just use the string version.

A workaround based on a MainActor-isolated task can't work due to conflict between compiler version and minimum iOS target:

    #if compiler(>=5.5) && canImport(_Concurrency)
    if #available(...) { // Check for availability of concurrency runtime
        Task { @mainactor [weak self] in
            NotificationCenter.default.addObserver(..., name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
        }
    } else {
        // IMPOSSIBLE CASE: No access to Task, no access to notification names
    }
    #else
    NotificationCenter.default.addObserver(..., name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
    #endif
But async apis are still only exposed to Swift 5.5.2.

Sendable conformances are helpful for users of Xcode < 13.2.
@groue groue marked this pull request as ready for review December 14, 2021 11:47
@groue groue merged commit e198472 into development Dec 14, 2021
@groue groue deleted the dev/async branch December 14, 2021 12:29
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.

4 participants