https://kotlinlang.org logo
Title
j

Joel Denke

05/23/2023, 7:12 AM
Whats the recommended way of sharing coroutine scopes/context between Android and iOS? In Android I am used to have app, io and computation in an injected interface or such, so can replace all in tests and such. But doesnt feels like thats working in iOS world with Swift. How do I solve this the best possible way, to be optimized threads?
Example of what I think of doing is like this:
actual val coroutineDispatchersModule = module {
    single(named("ioDispatcher")) { <http://Dispatchers.IO|Dispatchers.IO> }
    factory(named("ioScope")) { CoroutineScope(get(named("ioDispatcher"))) }
}
To inject coroutine scope each time need it, or only using dispatcher/context if need it. And then for compuation and main stuff as well. In iOS seems like there is some weird wrappers can use in darwin C wrappers, but not sure how to combine it with Coroutines to be sure not freezing issues or such.
In iOS world I THINK should be soemthing like this:
actual val coroutineDispatchersModule = module {
    single(named("ioDispatcher")) { IosMainDispatcher }
    factory(named("ioScope")) { CoroutineScope(get(named("ioDispatcher"))) }
}

object IosMainDispatcher : CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        dispatch_async(dispatch_get_main_queue()) { block.run() }
    }
}