diff --git a/Demo/PowerSyncExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Demo/PowerSyncExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 930e30e..7fa3fdb 100644 --- a/Demo/PowerSyncExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Demo/PowerSyncExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "25b8cd5d97789d7e497d6a5e0b04419a426018d83f0e80ab6817b213aa976748", + "originHash" : "1637bc26cc4eecc1f289454729470037184f1a8cab0de1c7caa7b1d5cb9faae9", "pins" : [ { "identity" : "anycodable", @@ -60,8 +60,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-collections", "state" : { - "revision" : "671108c96644956dddcd89dd59c203dcdb36cec7", - "version" : "1.1.4" + "revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb", + "version" : "1.1.0" } }, { diff --git a/Demo/PowerSyncExample/PowerSync/SystemManager.swift b/Demo/PowerSyncExample/PowerSync/SystemManager.swift index 21687b2..3822f8d 100644 --- a/Demo/PowerSyncExample/PowerSync/SystemManager.swift +++ b/Demo/PowerSyncExample/PowerSync/SystemManager.swift @@ -59,11 +59,11 @@ class SystemManager { func deleteList(id: String) async throws { try await db.writeTransaction(callback: { transaction in - _ = try await transaction.execute( + _ = transaction.execute( sql: "DELETE FROM \(LISTS_TABLE) WHERE id = ?", parameters: [id] ) - _ = try await transaction.execute( + _ = transaction.execute( sql: "DELETE FROM \(TODOS_TABLE) WHERE list_id = ?", parameters: [id] ) @@ -117,7 +117,7 @@ class SystemManager { func deleteTodo(id: String) async throws { try await db.writeTransaction(callback: { transaction in - _ = try await transaction.execute( + _ = transaction.execute( sql: "DELETE FROM \(TODOS_TABLE) WHERE id = ?", parameters: [id] ) diff --git a/Package.resolved b/Package.resolved index ad5061e..5a6fc12 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,14 +1,5 @@ { "pins" : [ - { - "identity" : "powersync-kotlin", - "kind" : "remoteSourceControl", - "location" : "https://github.com/powersync-ja/powersync-kotlin.git", - "state" : { - "revision" : "bedbece7be3576010830e0a7240979ec47de5526", - "version" : "1.0.0-BETA15.0" - } - }, { "identity" : "powersync-sqlite-core-swift", "kind" : "remoteSourceControl", diff --git a/Package.swift b/Package.swift index 4ed9122..4ee5d3f 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( targets: ["PowerSync"]), ], dependencies: [ - .package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA15.0"), + .package(name: "PowerSyncKotlin", path: "../powersync-kotlin"), .package(url: "https://github.com/powersync-ja/powersync-sqlite-core-swift.git", "0.3.8"..<"0.4.0") ], targets: [ @@ -26,7 +26,7 @@ let package = Package( .target( name: packageName, dependencies: [ - .product(name: "PowerSyncKotlin", package: "powersync-kotlin"), + .product(name: "PowerSyncKotlin", package: "PowerSyncKotlin"), .product(name: "PowerSyncSQLiteCore", package: "powersync-sqlite-core-swift") ]), .testTarget( diff --git a/Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift b/Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift index 8607767..dadf44d 100644 --- a/Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift +++ b/Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift @@ -123,38 +123,15 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol { } } - public func writeTransaction(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R { - return try await kotlinDatabase.writeTransaction(callback: SuspendTaskWrapper { transaction in - return try await callback(transaction) - }) as! R + public func writeTransaction(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R { + return try await kotlinDatabase.writeTransaction(callback: callback) as! R } - public func readTransaction(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R { - return try await kotlinDatabase.writeTransaction(callback: SuspendTaskWrapper { transaction in - return try await callback(transaction) - }) as! R + public func readTransaction(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R { + return try await kotlinDatabase.readTransaction(callback: callback) as! R } } enum PowerSyncError: Error { case invalidTransaction } - -class SuspendTaskWrapper: KotlinSuspendFunction1 { - let handle: (any PowerSyncTransaction) async throws -> Any - - init(_ handle: @escaping (any PowerSyncTransaction) async throws -> Any) { - self.handle = handle - } - - func __invoke(p1: Any?, completionHandler: @escaping (Any?, Error?) -> Void) { - Task { - do { - let result = try await self.handle(p1 as! any PowerSyncTransaction) - completionHandler(result, nil) - } catch { - completionHandler(nil, error) - } - } - } -} diff --git a/Sources/PowerSync/QueriesProtocol.swift b/Sources/PowerSync/QueriesProtocol.swift index 3c0564a..53d8e8e 100644 --- a/Sources/PowerSync/QueriesProtocol.swift +++ b/Sources/PowerSync/QueriesProtocol.swift @@ -39,10 +39,10 @@ public protocol Queries { ) -> AsyncStream<[RowType]> /// Execute a write transaction with the given callback - func writeTransaction(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R + func writeTransaction(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R /// Execute a read transaction with the given callback - func readTransaction(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R + func readTransaction(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R } extension Queries { diff --git a/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift b/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift index 96678c3..e15a13c 100644 --- a/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift +++ b/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift @@ -152,18 +152,17 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { func testWriteTransaction() async throws { try await database.writeTransaction { transaction in - _ = try await transaction.execute( + _ = transaction.execute( sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", parameters: ["1", "Test User", "test@example.com"] ) - _ = try await transaction.execute( + _ = transaction.execute( sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", parameters: ["2", "Test User 2", "test2@example.com"] ) } - let result = try await database.get( sql: "SELECT COUNT(*) FROM users", parameters: [] @@ -173,6 +172,33 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { XCTAssertEqual(result as! Int, 2) } + + func testWriteLongerTransaction() async throws { + let loopCount = 100 + + try await database.writeTransaction { transaction in + for i in 1...loopCount { + _ = transaction.execute( + sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", + parameters: [String(i), "Test User \(i)", "test\(i)@example.com"] + ) + + _ = transaction.execute( + sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", + parameters: [String(i*10000), "Test User \(i)-2", "test\(i)-2@example.com"] + ) + } + } + + let result = try await database.get( + sql: "SELECT COUNT(*) FROM users", + parameters: [] + ) { cursor in + cursor.getLong(index: 0) + } + + XCTAssertEqual(result as! Int, 2 * loopCount) + } func testReadTransaction() async throws { _ = try await database.execute( @@ -182,7 +208,7 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { try await database.readTransaction { transaction in - let result = try await transaction.get( + let result = transaction.get( sql: "SELECT COUNT(*) FROM users", parameters: [] ) { cursor in