Cyrille QUÉMIN
08/08/2019, 11:20 PMinterface 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
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 ioskpgalligan
08/08/2019, 11:34 PMCoroutinesInternalError
on freeze only? What is the crash you get otherwise?Cyrille QUÉMIN
08/08/2019, 11:43 PMCyrille QUÉMIN
08/08/2019, 11:48 PMpublic func connect(completionHandler: @escaping (Error?) -> Void) {
session?.connect(to: url!) { (error: Error?) in
completionHandler(error)
}
}
I had to do something like that:
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)
}
svyatoslav.scherbina
08/09/2019, 7:43 AM