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

Chenn

03/09/2020, 9:02 AM
What are the best practices for using coroutines in multiplatform?
l

louiscad

03/09/2020, 10:34 AM
Your question is too broad to be answer in chat. What do you miss exactly?
c

Chenn

03/09/2020, 10:49 AM
Not really looking for a specific, but more an abstract answer. I'm currently working on a jvm / js mpp where I'm trying to recreate redux and redux saga with coroutines. I'm putting all my async logic in the common and using it in js. But I'm student so kinda insecure about the choices I'm making atm.
l

louiscad

03/09/2020, 11:09 AM
I'm not familiar with redux, but I can tell you that MVI works well with suspending functions, regular functions, data classes and flows to abstract UI away. There's no state of the art about that yet though. I'm personally still figuring things out in that area. One thing that gained concensus though, is to expose and use suspending functions whenever it makes sense, and keep using suspending functions as long as it makes sense: no need to wrap them, take advantage of them to abstract details and complexity away.
😀 1
c

Chenn

03/09/2020, 11:15 AM
As far as my understanding, Redux is a npm library which manages the state of an application. Redux Saga is an add-on to redux which takes generator functions and have them execute asynchronous. I've tried to resemble these two architectures in kotlin by creating enum and data classes to manage the state and create a single suspending function which fires a coroutine with an "action" from the Action store (which is filled with functions that manipulate the enum and data classes). It works well, but I was in a lot of doubt about it, because it worked..
e

Etienne

03/09/2020, 5:30 PM
I’m currently looking at implementing “reactive patterns” in Kotlin MPP. It’s based off of work I’ve done with MVI. I’m at the stage where I have a similar “general questioning” going on. I.e. AFAIK Multi threaded coroutines are not available on KN, so how should I handle async work (like, say, a simple web call)
My understanding is that I’ll prob ably be able to get what I want from the existing primitives, but my goal is to unify approaches between JVM / Native
l

louiscad

03/10/2020, 8:14 AM
@Etienne You can use CoroutineWorker by Autodesk or make your own version until coroutines dispatcher switching becomes available right in kotlinx.coroutines for Kotlin/Native.
Also, ktor http client works asychronously on the main thread, so you can do all suspending functions already. And there's a main thread dispatcher for iOS somewhere on the web, Google should find it quick
e

Etienne

03/10/2020, 1:56 PM
👍 thank you Louis! 🙂