Is anyone using coroutines succesfully in iOS? Im ...
# multiplatform
c
Is anyone using coroutines succesfully in iOS? Im looking for an
actual
implementation for the Main and IO Dispatcher
r
s
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
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
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
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
I would stick these articles somewhere in official docs so everyone could read them beforehand.
k
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