Hi guys. I’m writing a multiplatform app with netw...
# multiplatform
a
Hi guys. I’m writing a multiplatform app with network calls and coroutines. I have basically two Coroutine Dispatchers, one for main and one for i/o operations. On Android, these are defined as Dispatchers.Main and Dispatchers.IO. On iOS, though, I’ve implemented what I’ve seen on many projects/tutorials online: a custom
dispatch_queue
dispatcher that takes a native dispatch queue and calls a
dispatch_async()
on it. The tutorials I’ve seen, though, they use the same dispatch queue for running every operation (the main dispatch queue). Indeed if I try to create a new dispatch queue for network (a new thread), it says it can’t run stuff in it because it can’t use a “non blocked or shared object” on a different thread. Questions: 1. Is there a way to create two threads for two dispatchers and use them with coroutines in Kotlin MPP (I think not, since I’m seeing that there’s an issue filed for it); 2. How bad is running network and UI in the same thread but asyncronously? 3. Are there some limitations for this?
k
1. No
2. That’s what ktor does
Network calls are async, and coroutine continues in main thread when async call completes
a
Thank you very much, now I’m relieved about it 😅
k
3. For async and continue? No real limitation, other than managing your own async. I have a pending blog post about this…
You call that from the main thread, give it a lambda (which will be frozen, so be careful), and it’ll suspend and run that in a background thread. When complete, it’ll continue in the main thread
All of this goes away when multithreaded coroutines happen
First 2 parts of threading series, while we’re talking about it https://medium.com/@kpgalligan/kotlin-native-stranger-threads-ep-1-1ccccdfe0c99
a
That’s very nice, looking forward to reading the new article when you’ll finish it! 😁