Skip to content

Commit 53227f5

Browse files
feat: update to latest kotlin and apply breaking changes (#15)
1 parent 419d2a2 commit 53227f5

File tree

12 files changed

+125
-108
lines changed

12 files changed

+125
-108
lines changed

CHANGELOG.md

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
# Changelog
22

3+
## 1.0.0-Beta.3
4+
5+
* BREAKING CHANGE: Update underlying powersync-kotlin package to BETA18.0 which requires transactions to become synchronous as opposed to asynchronous.
6+
```swift
7+
try await database.writeTransaction { transaction in
8+
try await transaction.execute(
9+
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
10+
parameters: ["1", "Test User", "[email protected]"]
11+
)
12+
}
13+
```
14+
to
15+
```swift
16+
try await database.writeTransaction { transaction in
17+
transaction.execute( // <- This has become synchronous
18+
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
19+
parameters: ["1", "Test User", "[email protected]"]
20+
)
21+
}
22+
```
23+
324
## 1.0.0-Beta.2
425

526
* Upgrade PowerSyncSqliteCore to 0.3.8

Demo/PowerSyncExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
"kind" : "remoteSourceControl",
1616
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
1717
"state" : {
18-
"revision" : "bedbece7be3576010830e0a7240979ec47de5526",
19-
"version" : "1.0.0-BETA15.0"
18+
"revision" : "074001ad7d02b2b70c77168cdf4958c08dd6121b",
19+
"version" : "1.0.0-BETA18.0"
2020
}
2121
},
2222
{
2323
"identity" : "powersync-sqlite-core-swift",
2424
"kind" : "remoteSourceControl",
2525
"location" : "https://github.com/powersync-ja/powersync-sqlite-core-swift.git",
2626
"state" : {
27-
"revision" : "a43d8c855a5461d0176135445eb6f425a3b38964",
28-
"version" : "0.3.8"
27+
"revision" : "5de629f7ddc649a1e89c64fde6113fe113fe14de",
28+
"version" : "0.3.9"
2929
}
3030
},
3131
{

Demo/PowerSyncExample/Components/AddListView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct AddListView: View {
1010
Section {
1111
TextField("Name", text: $newList.name)
1212
Button("Save") {
13-
Task.detached {
13+
Task {
1414
do {
1515
try await system.insertList(newList)
1616
await completion(.success(true))

Demo/PowerSyncExample/Components/AddTodoListView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct AddTodoListView: View {
1212
Section {
1313
TextField("Description", text: $newTodo.description)
1414
Button("Save") {
15-
Task.detached {
15+
Task{
1616
do {
1717
try await system.insertTodo(newTodo, listId)
1818
await completion(.success(true))

Demo/PowerSyncExample/Components/ListView.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ struct ListView: View {
6363
}
6464
}
6565
.task {
66-
Task {
67-
await system.watchLists { ls in
68-
withAnimation {
69-
self.lists = IdentifiedArrayOf(uniqueElements: ls)
70-
}
66+
await system.watchLists { ls in
67+
withAnimation {
68+
self.lists = IdentifiedArrayOf(uniqueElements: ls)
7169
}
7270
}
7371
}

Demo/PowerSyncExample/Components/TodoListView.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@ struct TodoListView: View {
6464
}
6565
}
6666
.task {
67-
Task {
68-
await system.watchTodos(listId) { tds in
69-
withAnimation {
70-
self.todos = IdentifiedArrayOf(uniqueElements: tds)
71-
}
67+
await system.watchTodos(listId) { tds in
68+
withAnimation {
69+
self.todos = IdentifiedArrayOf(uniqueElements: tds)
7270
}
7371
}
7472
}

Demo/PowerSyncExample/PowerSync/SystemManager.swift

+9-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SystemManager {
1111
func openDb() {
1212
db = PowerSyncDatabase(schema: schema, dbFilename: "powersync-swift.sqlite")
1313
}
14-
14+
1515
func connect() async {
1616
do {
1717
try await db.connect(connector: connector)
@@ -58,12 +58,12 @@ class SystemManager {
5858
}
5959

6060
func deleteList(id: String) async throws {
61-
try await db.writeTransaction(callback: { transaction in
62-
_ = try await transaction.execute(
61+
_ = try await db.writeTransaction(callback: { transaction in
62+
_ = transaction.execute(
6363
sql: "DELETE FROM \(LISTS_TABLE) WHERE id = ?",
6464
parameters: [id]
6565
)
66-
_ = try await transaction.execute(
66+
_ = transaction.execute(
6767
sql: "DELETE FROM \(TODOS_TABLE) WHERE list_id = ?",
6868
parameters: [id]
6969
)
@@ -116,14 +116,11 @@ class SystemManager {
116116
}
117117

118118
func deleteTodo(id: String) async throws {
119-
try await db.writeTransaction(callback: { transaction in
120-
_ = try await transaction.execute(
121-
sql: "DELETE FROM \(TODOS_TABLE) WHERE id = ?",
122-
parameters: [id]
123-
)
124-
return
119+
_ = try await db.writeTransaction(callback: { transaction in
120+
transaction.execute(
121+
sql: "DELETE FROM \(TODOS_TABLE) WHERE id = ?",
122+
parameters: [id]
123+
)
125124
})
126125
}
127126
}
128-
129-

Package.resolved

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
"kind" : "remoteSourceControl",
66
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
77
"state" : {
8-
"revision" : "bedbece7be3576010830e0a7240979ec47de5526",
9-
"version" : "1.0.0-BETA15.0"
8+
"revision" : "074001ad7d02b2b70c77168cdf4958c08dd6121b",
9+
"version" : "1.0.0-BETA18.0"
1010
}
1111
},
1212
{
1313
"identity" : "powersync-sqlite-core-swift",
1414
"kind" : "remoteSourceControl",
1515
"location" : "https://github.com/powersync-ja/powersync-sqlite-core-swift.git",
1616
"state" : {
17-
"revision" : "a43d8c855a5461d0176135445eb6f425a3b38964",
18-
"version" : "0.3.8"
17+
"revision" : "5de629f7ddc649a1e89c64fde6113fe113fe14de",
18+
"version" : "0.3.9"
1919
}
2020
}
2121
],

Package.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ let package = Package(
88
name: packageName,
99
platforms: [
1010
.iOS(.v13),
11-
.macOS(.v10_13)
1211
],
1312
products: [
1413
// Products define the executables and libraries a package produces, making them visible to other packages.
@@ -17,8 +16,8 @@ let package = Package(
1716
targets: ["PowerSync"]),
1817
],
1918
dependencies: [
20-
.package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA15.0"),
21-
.package(url: "https://github.com/powersync-ja/powersync-sqlite-core-swift.git", "0.3.8"..<"0.4.0")
19+
.package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA18.0"),
20+
.package(url: "https://github.com/powersync-ja/powersync-sqlite-core-swift.git", "0.3.9"..<"0.4.0")
2221
],
2322
targets: [
2423
// Targets are the basic building blocks of a package, defining a module or a test suite.

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

+5-28
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,16 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
122122
}
123123
}
124124
}
125-
126-
public func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R {
127-
return try await kotlinDatabase.writeTransaction(callback: SuspendTaskWrapper { transaction in
128-
return try await callback(transaction)
129-
}) as! R
125+
126+
public func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R {
127+
return try await kotlinDatabase.writeTransaction(callback: callback) as! R
130128
}
131129

132-
public func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R {
133-
return try await kotlinDatabase.writeTransaction(callback: SuspendTaskWrapper { transaction in
134-
return try await callback(transaction)
135-
}) as! R
130+
public func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R {
131+
return try await kotlinDatabase.readTransaction(callback: callback) as! R
136132
}
137133
}
138134

139135
enum PowerSyncError: Error {
140136
case invalidTransaction
141137
}
142-
143-
class SuspendTaskWrapper: KotlinSuspendFunction1 {
144-
let handle: (any PowerSyncTransaction) async throws -> Any
145-
146-
init(_ handle: @escaping (any PowerSyncTransaction) async throws -> Any) {
147-
self.handle = handle
148-
}
149-
150-
func __invoke(p1: Any?, completionHandler: @escaping (Any?, Error?) -> Void) {
151-
Task {
152-
do {
153-
let result = try await self.handle(p1 as! any PowerSyncTransaction)
154-
completionHandler(result, nil)
155-
} catch {
156-
completionHandler(nil, error)
157-
}
158-
}
159-
}
160-
}

Sources/PowerSync/QueriesProtocol.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public protocol Queries {
66
/// Execute a write query (INSERT, UPDATE, DELETE)
77
/// Using `RETURNING *` will result in an error.
88
func execute(sql: String, parameters: [Any]?) async throws -> Int64
9-
9+
1010
/// Execute a read-only (SELECT) query and return a single result.
1111
/// If there is no result, throws an IllegalArgumentException.
1212
/// See `getOptional` for queries where the result might be empty.
@@ -15,34 +15,34 @@ public protocol Queries {
1515
parameters: [Any]?,
1616
mapper: @escaping (SqlCursor) -> RowType
1717
) async throws -> RowType
18-
18+
1919
/// Execute a read-only (SELECT) query and return the results.
2020
func getAll<RowType>(
2121
sql: String,
2222
parameters: [Any]?,
2323
mapper: @escaping (SqlCursor) -> RowType
2424
) async throws -> [RowType]
25-
25+
2626
/// Execute a read-only (SELECT) query and return a single optional result.
2727
func getOptional<RowType>(
2828
sql: String,
2929
parameters: [Any]?,
3030
mapper: @escaping (SqlCursor) -> RowType
3131
) async throws -> RowType?
32-
32+
3333
/// Execute a read-only (SELECT) query every time the source tables are modified
3434
/// and return the results as an array in a Publisher.
3535
func watch<RowType>(
3636
sql: String,
3737
parameters: [Any]?,
3838
mapper: @escaping (SqlCursor) -> RowType
3939
) -> AsyncStream<[RowType]>
40-
40+
4141
/// Execute a write transaction with the given callback
42-
func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R
42+
func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R
4343

4444
/// Execute a read transaction with the given callback
45-
func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R
45+
func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R
4646
}
4747

4848
extension Queries {

0 commit comments

Comments
 (0)