You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: client-sdk-references/swift.mdx
+46-32Lines changed: 46 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,26 +4,33 @@ sidebarTitle: "Overview"
4
4
---
5
5
6
6
<Note>
7
-
The PowerSync Swift SDK is currently in an alpha release. It is not suitable for production use at this stage unless you've tested your use cases extensively. Breaking changes are still likely to occur.
7
+
The PowerSync Swift SDK is currently in an alpha release. It is not suitable for production use at this stage unless you've tested your use cases extensively. Breaking changes are still likely to occur.
8
8
</Note>
9
9
10
10
## Kotlin Multiplatform -> Native Swift SDK
11
11
12
12
This SDK is an extension of the [Kotlin Multiplatform SDK](https://github.com/powersync-ja/powersync-kotlin), and utilizes the API tool [SKIE](https://skie.touchlab.co/) to generate and publish a [PowerSync SDK](https://github.com/powersync-ja/powersync-kotlin/tree/main/PowerSync) that can be used in a standalone Swift app. Further details about this configuration can be found in our blog [here](https://www.powersync.com/blog/using-kotlin-multiplatform-with-kmmbridge-and-skie-to-publish-a-native-swift-sdk).
13
13
14
-
<Info>
15
-
**Demo App**
14
+
For the beta, we are moving away from this Kotlin-based architecture towards more Swift-native APIs. For the stable release, will are planning a fully native Swift SDK.
16
15
17
-
We recommend the [Supabase Todo List Demo](https://github.com/powersync-ja/powersync-kotlin-swift-demo) app as a starting point for using the Swift SDK. See the README for details to run it.
18
-
</Info>
16
+
<Tip>
17
+
**Demo App**
18
+
19
+
We recommend the [Supabase Todo List Demo](https://github.com/powersync-ja/powersync-kotlin-swift-demo) app as a starting point for using the Swift SDK. See the README for details to run it.
20
+
</Tip>
19
21
20
22
### SDK Features
21
23
22
24
* Provides real-time streaming of database changes.
25
+
23
26
* Offers direct access to the SQLite database, enabling the use of SQL on both client and server sides.
27
+
24
28
* Operations are asynchronous by default, ensuring the user interface remains unblocked.
29
+
25
30
* Supports concurrent database operations, allowing one write and multiple reads simultaneously.
31
+
26
32
* Enables subscription to queries for receiving live updates.
33
+
27
34
* Eliminates the need for client-side database migrations as these are managed automatically.
28
35
29
36
## Installation
@@ -61,41 +68,42 @@ let package = Package(
61
68
)
62
69
```
63
70
64
-
2. Using Xcode:
71
+
1. Using Xcode:
72
+
<Frame>
73
+
<imgsrc="/images/swift.avif" />
74
+
</Frame>
75
+
<Frame>
76
+
<imgsrc="/images/image (1).png" />
77
+
</Frame>
65
78
66
-
1. Follow [this](https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app#Add-a-package-dependency) guide to add a package to your project.
67
-
2. When searching for the package use [https://github.com/powersync-ja/powersync-kotlin.git](https://github.com/powersync-ja/powersync-kotlin.git) as the URL and include the exact version `1.0.0-BETA5.0` as shown below:
79
+
1. Do the same for `power-sqlite-core-swift` using [https://github.com/powersync-ja/powersync-sqlite-core-swift.git](https://github.com/powersync-ja/powersync-kotlin.git) and choose a range between `0.3.1` and `0.4.0` as shown below:
68
80
69
-
<Frame>
70
-
71
-
<imgsrc="/images/swift.avif"></img>
72
-
</Frame>
81
+
1) Follow [this](https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app#Add-a-package-dependency) guide to add a package to your project.
73
82
74
-
3. Do the same for `power-sqlite-core-swift` using [https://github.com/powersync-ja/powersync-sqlite-core-swift.git](https://github.com/powersync-ja/powersync-kotlin.git) and choose a range between `0.3.1` and `0.4.0` as shown below:
75
-
76
-
<Frame>
77
-
<imgsrc="/images/image (1).png"></img>
78
-
</Frame>
83
+
2) When searching for the package use [https://github.com/powersync-ja/powersync-kotlin.git](https://github.com/powersync-ja/powersync-kotlin.git) as the URL and include the exact version `1.0.0-BETA5.0` as shown below:
79
84
80
85
<Note>
81
-
Use the exact version for `powersync-kotlin` as we are actively making changes to the Swift SDK which could result in breaking changes. The version history can be found [here](https://central.sonatype.com/artifact/com.powersync/core/versions).
86
+
Use the exact version for `powersync-kotlin` as we are actively making changes to the Swift SDK which could result in breaking changes. The version history can be found [here](https://central.sonatype.com/artifact/com.powersync/core/versions).
82
87
</Note>
83
88
84
89
## Getting Started
85
90
86
91
Before implementing the PowerSync SDK in your project, make sure you have completed these steps:
87
92
88
93
* Signed up for a PowerSync Cloud account ([here](https://accounts.journeyapps.com/portal/get-started?powersync=true)) or [self-host PowerSync](/self-hosting/getting-started).
94
+
89
95
*[Configured your backend database](/installation/database-setup) and connected it to your PowerSync instance.
96
+
90
97
*[Installed](/client-sdk-references/swift#installation) the PowerSync SDK.
91
98
92
-
### 1\. Define the Schema
99
+
### 1. Define the Schema
93
100
94
101
The first step is defining the schema for the local SQLite database, which is provided to the `PowerSyncDatabase` constructor via the `schema` parameter. This schema represents a "view" of the downloaded data. No migrations are required — the schema is applied directly when the PowerSync database is constructed.
95
102
96
103
The types available are `text`, `integer` and `real`. These should map directly to the values produced by the [Sync Rules](/usage/sync-rules). If a value doesn't match, it is cast automatically.
97
104
98
105
**Example**:
106
+
99
107
```swift
100
108
importFoundation
101
109
importPowerSync
@@ -148,10 +156,10 @@ let AppSchema = Schema(tables: [lists, todos])
148
156
```
149
157
150
158
<Info>
151
-
**Note**: No need to declare a primary key `id` column, as PowerSync will automatically create this.
159
+
**Note**: No need to declare a primary key `id` column, as PowerSync will automatically create this.
152
160
</Info>
153
161
154
-
### 2\. Instantiate the PowerSync Database
162
+
### 2. Instantiate the PowerSync Database
155
163
156
164
Next, you need to instantiate the PowerSync database — this is the core managed database.
157
165
@@ -172,20 +180,22 @@ var db = PowerSyncDatabase(
172
180
)
173
181
```
174
182
175
-
### 3\. Integrate with your Backend
183
+
### 3. Integrate with your Backend
176
184
177
185
Create a connector to integrate with your backend. The PowerSync backend connector provides the connection between your application backend and the PowerSync managed database.
178
186
179
187
It is used to:
180
188
181
189
1. Retrieve an auth token to connect to the PowerSync instance.
190
+
182
191
2. Apply local changes on your backend application server (and from there, to Postgres)
183
192
184
193
Accordingly, the connector must implement two methods:
185
194
186
-
1.`PowerSyncBackendConnector.fetchCredentials`\- This is called every couple of minutes and is used to obtain credentials for your app's backend API. -> See [Authentication Setup](/installation/authentication-setup) for instructions on how the credentials should be generated.
187
-
2.`PowerSyncBackendConnector.uploadData`\- Use this to upload client-side changes to your app backend.
188
-
\-> See [Writing Client Changes](/installation/app-backend-setup/writing-client-changes) for considerations on the app backend implementation.
195
+
1.`PowerSyncBackendConnector.fetchCredentials` - This is called every couple of minutes and is used to obtain credentials for your app's backend API. -> See [Authentication Setup](/installation/authentication-setup) for instructions on how the credentials should be generated.
196
+
197
+
2.`PowerSyncBackendConnector.uploadData` - Use this to upload client-side changes to your app backend.
198
+
-> See [Writing Client Changes](/installation/app-backend-setup/writing-client-changes) for considerations on the app backend implementation.
189
199
190
200
**Example**:
191
201
@@ -218,7 +228,7 @@ class MyConnector: PowerSyncBackendConnector {
218
228
```
219
229
220
230
<Info>
221
-
If you are using **Supabase**, you can use [SupabaseConnector.swift](https://github.com/powersync-ja/powersync-kotlin-swift-demo/blob/main/PowerSyncExample/PowerSync/SupabaseConnector.swift) as a starting point.
231
+
If you are using **Supabase**, you can use [SupabaseConnector.swift](https://github.com/powersync-ja/powersync-kotlin-swift-demo/blob/main/PowerSyncExample/PowerSync/SupabaseConnector.swift) as a starting point.
222
232
</Info>
223
233
224
234
## Using PowerSync: CRUD functions
@@ -227,11 +237,15 @@ Once the PowerSync instance is configured you can start using the SQLite DB func
227
237
228
238
The most commonly used CRUD functions to interact with your SQLite data are:
229
239
230
-
*[PowerSyncDatabase.get](/client-sdk-references/swift#fetching-a-single-item)\- get (SELECT) a single row from a table.
231
-
*[PowerSyncDatabase.getOptional](/client-sdk-references/swift#fetching-a-single-item)\- get (SELECT) a single row from a table and return `null` if not found.
232
-
*[PowerSyncDatabase.getAll](/client-sdk-references/swift#querying-items-powersync-getall)\- get (SELECT) a set of rows from a table.
233
-
*[PowerSyncDatabase.watch](/client-sdk-references/swift#watching-queries-powersync-watch)\- execute a read query every time source tables are modified.
234
-
*[PowerSyncDatabase.execute](/client-sdk-references/swift#mutations-powersync-execute)\- execute a write (INSERT/UPDATE/DELETE) query.
240
+
*[PowerSyncDatabase.get](/client-sdk-references/swift#fetching-a-single-item) - get (SELECT) a single row from a table.
241
+
242
+
*[PowerSyncDatabase.getOptional](/client-sdk-references/swift#fetching-a-single-item) - get (SELECT) a single row from a table and return `null` if not found.
243
+
244
+
*[PowerSyncDatabase.getAll](/client-sdk-references/swift#querying-items-powersync-getall) - get (SELECT) a set of rows from a table.
245
+
246
+
*[PowerSyncDatabase.watch](/client-sdk-references/swift#watching-queries-powersync-watch) - execute a read query every time source tables are modified.
247
+
248
+
*[PowerSyncDatabase.execute](/client-sdk-references/swift#mutations-powersync-execute) - execute a write (INSERT/UPDATE/DELETE) query.
235
249
236
250
### Fetching a Single Item ( PowerSync.get / PowerSync.getOptional)
0 commit comments