Skip to content

Conversation

stevensJourney
Copy link
Contributor

@stevensJourney stevensJourney commented Aug 21, 2025

Overview

It's currently tricky to throw Errors from Swift closure functions which are provided to the Kotlin SDK.

We currently achieve exception propagation for the readLock, readTransaction, writeLock and writeTransaction methods by implementing TransactionCallback and LockCallback wrappers on the Swift side. These wrappers:

  • Run the provided Swift closure and catch any thrown exceptions
  • Return the exception as the result of the wrapped closure as a PowerSyncException instance
  • The Kotlin SDK then detects this exception and throws the exception on the Kotlin side

SKIEE has known limitations with generics. Which causes some rather strange requirements for the TransactionCallback and LockCallback implementations.

The execute method of these implementations should be able to return Any? (a nullable response). But this is not possible due to Objective C limitations:

Throwing method cannot be an implementation of an '@objc' requirement because it returns a value of optional type 'Any?'; 'nil' indicates failure to Objective-CSourceKit

This causes us to define the execute method as returning Any which works, but prints a warning on every compile.

func execute(transaction: PowerSyncKotlin.PowerSyncTransaction) throws -> Any {
          - warning: result of 'execute(transaction:)' has different optionality than expected by protocol 'ThrowableTransactionCallback'
52          do {
53              return try callback(

Furthermore. When enabling the Swift 6 toolchain while making Swift 6 declaration changes. The compiler seems to crash when processing the TransactionCallback.swift file.

This PR removes the need for TransactionCallback and LockCallback in the Swift SDK, by making use of the changes in Kotlin PR

@stevensJourney stevensJourney requested a review from Copilot August 21, 2025 09:50
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves lock error handling in the Swift PowerSync SDK by removing the need for custom TransactionCallback and LockCallback wrapper classes. The changes address Swift/Objective-C compatibility issues and compiler warnings related to optionality mismatches.

  • Removes custom callback wrapper classes that had Swift 6 compatibility issues
  • Replaces callback wrappers with new Kotlin SDK wrapper functions
  • Introduces standardized error handling using PowerSyncResult enum

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
Sources/PowerSync/Kotlin/TransactionCallback.swift Completely removes the file containing custom callback wrapper classes
Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift Updates method calls to use new wrapper functions and adds helper functions for error handling

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@stevensJourney stevensJourney marked this pull request as ready for review August 25, 2025 07:17
simolus3
simolus3 previously approved these changes Aug 25, 2025
Copy link
Contributor

@simolus3 simolus3 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 good to me, I also like that this lets us remove Swift-specific logic from the :core Kotlin project.

@stevensJourney stevensJourney merged commit 2de1a47 into main Aug 26, 2025
3 checks passed
@stevensJourney stevensJourney deleted the lock-improvements branch August 26, 2025 06:56
@stevensJourney stevensJourney mentioned this pull request Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants