toomanyeduardos
12/01/2018, 3:45 AMfun
and the other one is a suspend fun
, which is using coroutines to make a network call.
on android, i can see both functions from this class. i can call them, and life is good.
on ios, I can only see the function that is a regular fun
. the suspend fun
is not visible at all.
what did i miss? does iOS not support suspending functions from coroutines?yuya_horita
12/01/2018, 3:47 AMsuspend fun
is not exported to .framework
on iOS at this time.toomanyeduardos
12/01/2018, 3:47 AMyuya_horita
12/01/2018, 3:48 AMtoomanyeduardos
12/01/2018, 3:48 AMyuya_horita
12/01/2018, 3:48 AMyuya_horita
12/01/2018, 3:48 AMjonnyzzz
12/01/2018, 3:48 AMsuspend
function as a function with callback, that is executed when function is completed. You cannot call such function from Java, and objc.
The workaround is wrap the coroutine with an ordinary function.
https://github.com/Kotlin/kotlinx.coroutines/issues/470#issuecomment-440080970
That may helptoomanyeduardos
12/01/2018, 3:48 AMyuya_horita
12/01/2018, 3:49 AMyuya_horita
12/01/2018, 3:51 AMsuspend fun hello(): String {
try {
return helloRepository.hello()
} catch (e: Throwable) {
throw e
}
}
yuya_horita
12/01/2018, 3:51 AMyuya_horita
12/01/2018, 3:51 AMfun HelloService.sendHello(block: (String?, Error?) -> Unit) {
launch(block) {
this@sendHello.hello()
}
}
yuya_horita
12/01/2018, 3:52 AMtoomanyeduardos
12/01/2018, 3:52 AMyuya_horita
12/01/2018, 3:53 AMCoroutineDispathcer
yuya_horita
12/01/2018, 3:54 AMinternal class MainScope: CoroutineScope {
private val context = MainDispatcher()
private val job = Job()
private val exceptionHandler = CoroutineExceptionHandler { _, _ -> }
override val coroutineContext: CoroutineContext
get() = context + job + exceptionHandler
}
private class MainDispatcher: CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatch_async(dispatch_get_main_queue()) { block.run() }
}
}
yuya_horita
12/01/2018, 3:54 AMtoomanyeduardos
12/01/2018, 3:54 AMyuya_horita
12/01/2018, 3:55 AMyuya_horita
12/01/2018, 3:55 AMtoomanyeduardos
12/01/2018, 3:55 AMtoomanyeduardos
12/01/2018, 3:55 AMyuya_horita
12/01/2018, 3:56 AMyuya_horita
12/01/2018, 3:56 AMyuya_horita
12/01/2018, 3:56 AMinternal fun <T> launch(block: (T?, Error?) -> Unit, suspendBlock: suspend CoroutineScope.() -> T) {
MainScope().launch {
try {
val response = suspendBlock()
block(response, null)
} catch (e: Error) {
block(null, e)
}
}
}
toomanyeduardos
12/01/2018, 3:56 AMyuya_horita
12/01/2018, 3:57 AMyuya_horita
12/01/2018, 3:58 AMtoomanyeduardos
12/01/2018, 3:58 AMyuya_horita
12/01/2018, 4:00 AMsendHello
functionyuya_horita
12/01/2018, 4:00 AMin Swift
Services.helloService.sendHello { response, error in
}
sendHello is just a wrapper for hello()
suspend function.toomanyeduardos
12/01/2018, 4:00 AMyuya_horita
12/01/2018, 4:01 AM