public fun launchAndCatch(
onError: (Throwable) -> Unit,
function: suspend () -> Unit): Finalizable {
val ret = Finalizable()
val coroutineContext: CoroutineContext = Dispatchers.Main
GlobalScope.launch(coroutineContext) {
try {
function()
} catch (e: Throwable) {
onError(e)
} finally {
ret.onFinally?.invoke()
}
}
return ret
}
Jaroslav
10/24/2018, 12:49 PM
I’ve just modfied it a little to use main thread in lounchAndCatch funcion
Jaroslav
10/24/2018, 1:43 PM
NOw i’m trying to send context from ios code like you do, but in my generated framework there is no such class
Kotlinx_coroutines_core_nativeCoroutineDispatcher
. How can I make it compile this with a library?
s
Sam
10/24/2018, 2:42 PM
I had the same problem as I had used the code from the KotlinConf app. The trick is to have a public property that forces the K/N header generator to include it in the framework header. IIRC when I made the ktor HTTPClient visible the coroutine types were exposed in the framework header.
j
Jaroslav
10/24/2018, 2:48 PM
cool! added unused property and not it’s there! 🙂 thanks
Jaroslav
10/24/2018, 2:52 PM
have you tried changing dispatch queue to some other, non-main?
s
Sam
10/24/2018, 3:27 PM
I have not. It was a weekend coding project for a conference talk I did and sadly, I haven’t been able to get back to playing with it since.