https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
c

carlos cdmp

07/31/2019, 1:04 PM
Is anyone using coroutines succesfully in iOS? Im looking for an
actual
implementation for the Main and IO Dispatcher
r

russhwolf

07/31/2019, 1:41 PM
s

Sergioedcigreja

07/31/2019, 1:57 PM
actual val ApplicationDispatcher: CoroutineDispatcher = NsQueueDispatcher(dispatch_get_main_queue()) internal class NsQueueDispatcher( private val dispatchQueue: dispatch_queue_t ) : CoroutineDispatcher() { override fun dispatch(context: CoroutineContext, block: Runnable) { dispatch_async(dispatchQueue) { block.run() } } }
c

carlos cdmp

07/31/2019, 1:59 PM
Ok
my implementation was like that already
but looks like in android im able to come back to main thread to set the view label, but in ios it doesnt work
if someone have any idea it would really help
l

louiscad

07/31/2019, 2:14 PM
First result on Google for "kotlin ios main dispatcher" brings a GitHub issue, where the most positively reacted to comment is this one with a read to copy-paste solution for macOS and iOS: https://github.com/Kotlin/kotlinx.coroutines/issues/470#issuecomment-440080970
It supports cancellation and delay, which are very important.
k

kpgalligan

07/31/2019, 2:20 PM
I’m definitely no expert on coroutines in practice, but you probably don’t want to be using Global? https://github.com/carlosdmp/recipe-multiplatform/blob/master/app/src/commonMain/kotlin/presentation/RecipePresenter.kt
In any case, just reading it, it looks like you’re passing data through closures from something in a background thread to the main thread, which is simply not going to work like that.
You need to understand native threading and state rules first, and/or wait out the result of the multithreaded coroutines and “relaxed mode” https://github.com/Kotlin/kotlinx.coroutines/issues/462
a

Arkadii Ivanov

07/31/2019, 3:39 PM
I would stick these articles somewhere in official docs so everyone could read them beforehand.
k

kpgalligan

07/31/2019, 3:39 PM
So would I, but I am biased
I should have them in my bookmarks at least. I need to look them up whenever I post them
3 Views