Question on the Swift async to Kotlin suspend func...
# multiplatform
s
Question on the Swift async to Kotlin suspend functionality. Given a Kotlin function like this:
Copy code
suspend fun anyfunc(arg: String, block: suspend (arg:String) -> Bool) {
        // any code in here
    }
Kotlin's Swift mapping of the above requires implementing KotlinSuspendFunction<n> in Swift to pass the
block
lambda, which all works fine. See the Swift code below. My question is about an unexpected thread change on return from calling
anyfunc
, see the Swift code:
Copy code
func xxx() async {
    var isMain = Thread.isMainThread               // true here
    try await anyfunc("xxx", block: LambdaImpl() )
    isMain = Thread.isMainThread                   // false !! why?

    // next call to any other Kotlin suspend function from Swift fails, see exception below	
}

class LambdaImpl: : KotlinSuspendFunction2 {
    func invoke(p1: Any?) async throws -> Any? {
        //p1 is the "xxx" string arg above
        // can do any logic here that doesn't invoke suspend
        KotlinBoolean(bool: true)      // return arg
    }
}
Exception :`2022-02-03 134151.199088-0800 iosApp[2072:53407] * Terminating app due to uncaught exception 'NSGenericException', reason: 'Calling Kotlin suspend functions from Swift/Objective-C is currently supported only on main thread'` Anyone know why the thread changes, and if there is a way to stop that from happening? I figured the lambda code might be dispatched differently, but didn't expect the thread to change in the middle of func xxx. The main thread is required for the next call to a suspend function, if not it throws an Objective C exception because Kotlin suspend functions can only be called on the main thread. Thanks in advance for any help!