what is the recommended way of achieving callbacks...
# multiplatform
a
what is the recommended way of achieving callbacks in Kotlin Multiplatform? Via coroutines?
z
are you talking about at the borders of your shared code and platform code?
a
For example for interfaces in the common code, that are implemented in platform specific code
z
gotcha yeah so if you're using 1.3, coroutines will work fine on Android, but you end up needing to use callbacks on other platforms
s
We have a KMP supporting Android and iOS. We have a class called
Observer
that is in the common shared code and accepts a plain lambda for iOS or a suspend lambda for Android. These lambdas are the callbacks for `Flow`s emitting data, or, if you want, for a suspend-fun returning a value.
a
why needing to use callbacks on other platforms? coroutines don't have an equivalent on iOS for example?
z
correct, swift doesn't have a coroutine like facility yet
a
are there any samples online around this?
b
This is the setup I use with just pure coroutines. Check out the "Presentation layer" section. Much more flexible than callbacks and I find it cleaner than observers. It also works on iOS well.
a
Thanks for sharing. Went over quickly, but mentions "We don’t show any loading indicator, because there’s on big issue: Kotlin/Native does not support multithreading at the moment. This means, all of our code has to run on the main thread, therefore a loading indicator doesn’t make much sense when we’re doing network on the main thread." - so you're doing everything on main thread on iOS?
s
There are experimental coroutine lib versions with native mt:
1.3.5-native-mt
and a few other versions…., they are mentioned in that 462 issue
b
Oh yeah, I forgot about that. I had to implemented a custom iOS dispatcher with code I lifted from here that will allow async coroutines. Then create your coroutine scopes using that dispatcher. I'm not sure how the experimental lib works though, as Anton pointed out.
s
The experimental lib provides the multi-threaded
Dispatchers.Default
to your common code. If you are interested in doing KMP for Android and iOS only, take a look at TouchLab’s KaMP-Kit for guidance.
b
^^ yeah, if you can get that working it's for sure better than my hack.
p
so does 1.3.5-native-mt actually provide multithreading on iOS? i couldn't find any documentation on the release and have been reading through github for a while trying to figure it out
r
p
cool, so it sounds like at least some basic ability to run coroutines on a background thread is supported on all platforms with Dispatchers.Default implemented, with the caveat that it's currently a single background thread.. does that sound right?
r
Also with the caveat that it doesn't rescue you from needing to handle things getting frozen when crossing threads
p
thanks, that's very helpful
and what does 1.4.0-mt add?
r
1.4 support and whatever bugs they've fixed in the meantime. If you want more detail you're mostly stuck digging through issues and commit messages (or looking at the code directly)
p
yeah, i've been doing that quite a bit, was hoping to get my head out of the weeds to see if someone could help at a higher level.