I tried something as bellow. In the common module ...
# kotlin-native
c
I tried something as bellow. In the common module I have:
Copy code
interface View{
  suspend fun myFunSuspend().
}

// for Android, normal implementation of co routine
// for iOS see below, because I need to call a function in swift that uses callbacks

class iOSViewWrapper(private val iOSImpl: IosView) : View {

  override suspend fun myFunSuspend(){
    return suspendCoroutine {
      iOSImpl.doAsyncJob{ data, error ->
        if(error != null){
          it.resumeWithException(error)
        }else{
          it.resume(data)
        }
      }
    } 
  }
}

interface IosView {
  fun doAsyncJob((data: ByteArray, error: Throwable?)-> Unit)
}
the function
doAsyncJob
implementation is done in Swift and is called many times inside my kotlin code. I have defined a coroutine scope that is using the ios
dispatch_get_main_queue
. I have made sure that
doAsyncJob
is execute AND invoke the closure on the main thread. The code works but crashes always in the swift invocation of the closure. Sometimes after 1 call sometimes after 6 or 10, ... I tried to declare the closure of
doAsyncJob
as a variable and call
.freeze()
on it, but I get
Copy code
kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[MainDispatcher@7d793018, Continuation @ $prepareCOROUTINE$11]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
        at 0   mylib                            0x0000000100f81e98 kfun:kotlin.Error.<init>(kotlin.String?;kotlin.Throwable?)kotlin.Error + 24
        at 1   mylib                            0x0000000100f81bc0 kfun:kotlinx.coroutines.CoroutinesInternalError.<init>(kotlin.String;kotlin.Throwable)kotlinx.coroutines.CoroutinesInternalError + 24
        at 2   mylib                            0x0000000100f81818 kfun:kotlinx.coroutines.DispatchedTask.handleFatalException$kotlinx-coroutines-core(kotlin.Throwable?;kotlin.Throwable?) + 284
        at 3   mylib                            0x0000000100f82540 kfun:kotlinx.coroutines.DispatchedTask.run() + 1224
what am I doing wrong? I am using kotlin
1.3.41
and coroutine
1.3.0-RC
with an mpp project android and ios
k
You get the
CoroutinesInternalError
on freeze only? What is the crash you get otherwise?
c
yes, the crash I get otherwise is BAD_ACCESS on the swift side
the only workaround I have just found is instead of declaring the closure inline in swift, I assign them to a class variable, pretty ugly codewise but it works…. instead of this that looks neater but crashes:
Copy code
public func connect(completionHandler: @escaping (Error?) -> Void) {
  session?.connect(to: url!) { (error: Error?) in
    completionHandler(error)
  }
}
I had to do something like that:
Copy code
private var chConnect: ((Error?) -> Void)? = nil
public func connect(completionHandler: @escaping (Error?) -> Void) {
    chConnect = completionHandler
    session?.connect(to: url!, completionHandler: handleConnection(error:))
}

private func handleConnection(error: Error?){
  chConnect?(error)
}
s