Skip to content

Implement connection pool in Swift, remove Kotlin framework#130

Merged
simolus3 merged 48 commits intomainfrom
swift-pool
May 6, 2026
Merged

Implement connection pool in Swift, remove Kotlin framework#130
simolus3 merged 48 commits intomainfrom
swift-pool

Conversation

@simolus3
Copy link
Copy Markdown
Contributor

@simolus3 simolus3 commented Apr 23, 2026

This implements SqlCursor and connection pools directly in Swift instead of depending on a Kotlin XCFramework. On a high level, this PR:

  • Moves some protocol methods into extensions. For instance, the get$Type(name:) functions on SqlCursor are now implemented by delegating to get$Type(index:).
  • Implement SqlCursor and related methods using CSQLite instead of using the Kotlin framework.
  • Adds a Swift implementation of SQLiteConnectionPoolProtocol responsible for opening connections, configuring them and to dispatch queries on a background thread. The actual implementation is close to what we have on JavaScript, with an AsyncSemaphore helper guarding connections.
  • Uses a direct function call to load the core extension! We can let the OS loader take care of loading the core extension framework, avoiding sqlite3_load_extension and having to resolve the path manually.

For the native SQLite wrapper, the implementation consists of these structs:

  • NativeSqliteStatement is an owning (~Copyable) struct managing a SQLite statement and exposing raw SQLite methods to step through it.
  • StatementCursor implements the existing SqlCursor protocol with a dynamic reference to a NativeSqliteStatement. Ideally, I would have preferred this to be ~Copyable as well and change APIs to give users a borrowing SqlCursor in callbacks for safety. That would be breaking though, so we instead have manual checks to verify a cursor isn't used outside of its callback.
  • NativeConnectionPool manages a write connection (and an optional collection of readers). These are implemented through the owning RawSqliteConnection struct and an AsyncSemaphore with a design taken from our JS SDK implementation. We use the core extension with powersync_update_hooks to dispatch update notifications. For simplicity, this class is not responsible for opening connections or dispatching writes to a background thread.
  • AsyncConnectionPool is responsible for opening connections and supports both in-memory databases and files in the default database directory (the design is so that we can potentially support custom paths in the future as well). It wraps a NativeConnectionPool for concurrency control and invokes callbacks on DispatchQueue.global(qos:)` to avoid blocking the main thread.
  • PowerSyncDatabaseImpl implements high-level PowerSync APIs by opening a pool and running the usual initial statements to resolve the initial sync state.

Most of this is covered by existing tests, I've added a simple additional test covering how we bind parameter values.

Base automatically changed from swift-sync-client to main April 29, 2026 07:06
@simolus3 simolus3 marked this pull request as ready for review April 29, 2026 08:27
@simolus3 simolus3 requested a review from stevensJourney April 29, 2026 08:27
@simolus3 simolus3 marked this pull request as draft April 30, 2026 15:58
@mintlify
Copy link
Copy Markdown

mintlify Bot commented May 4, 2026

Docs PR opened: powersync-ja/powersync-docs#419

Opened a PR adding a docs-update tracker flagging that the Swift SDK is now pure Swift (no Kotlin/SKIE), so the client-sdks/reference/swift.mdx "Kotlin -> Swift SDK" section and CRUD-type samples need updating.

Copy link
Copy Markdown
Contributor

@stevensJourney stevensJourney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very nice and clean. I'm happy with these changes.

We should make a note to also check our docs which reference the Kotlin SDK e.g. https://docs.powersync.com/client-sdks/reference/swift#kotlin-%3E-swift-sdk . It looks like there was an autogenerated PR for this, but that was closed.

@simolus3
Copy link
Copy Markdown
Contributor Author

simolus3 commented May 6, 2026

We should make a note to also check our docs which reference the Kotlin SDK

Good catch, I'll prepare releases and will update the docs for that.

@simolus3 simolus3 merged commit 71ae180 into main May 6, 2026
3 checks passed
@simolus3 simolus3 deleted the swift-pool branch May 6, 2026 11:38
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.

3 participants