DEV Community

Discussion on: Maximizing Code Sharing between Android and iOS with Kotlin Multiplatform

Collapse
 
baudouxbenjamin profile image
Benjamin

Hi Kurt! Great article but I still have a question:

"Suspending functions aren't compiled to ObjC so we can't use those on iOS" and yet you defined the following function

suspend fun getItems(): List = client.get("url.only.fortest/items")

which is a suspending function. Are they compiled to Objective-C or not?

Collapse
 
kuuurt profile image
Kurt Renzo Acosta

You can still use Coroutines but only in Kotlin. You won't be able to call them in Objective-C.

Collapse
 
baudouxbenjamin profile image
Benjamin • Edited

So what's the point of the getItems() function if it cannot be called on iOS? Or is it meant to be used only inside the multi-platform code itself?

Thread Thread
 
kuuurt profile image
Kurt Renzo Acosta • Edited

It's being called on Kotlin's side through the ViewModel. The ViewModel calls it and exposes it through a CFlow which is just a wrapper of a Flow with an extra watch function that we can use in Objective-C.

Edit: More explanation (I was on a phone earlier and it was hard)

To consume a Flow, you can use collect which has this signature suspend fun Flow<*>.collect(): Unit. Since this is a suspending function, you won't be able to call this in Objective-C. suspend functions' interoperability are still unsupported so we expose a normal watch function that takes a lambda and handles launching the coroutine.

I take no credit on the watch function. The guys from Jetbrains did a good job on it. github.com/JetBrains/kotlinconf-ap...