https://kotlinlang.org logo
Title
m

Marc Reichelt

02/22/2023, 12:52 PM
Hey there! • Using Flows from KMM in Swift is possible thanks to https://github.com/rickclephas/KMP-NativeCoroutines • But: how can we pass Swift publishers (AnyPublisher<Boolean>) to KMM as Flows?
Say we have this code in Kotlin:
fun doSomething(flow: Flow<Boolean>)
Now we have a publisher like this in Swift:
func swiftFunction(publisher: AnyPublisher<Boolean, Never>) {
   doSomething(publisher.asFlow())
}
Is there anything like
asFlow()
(which currently is not available) - or what would you do instead?
r

Rick Clephas

02/22/2023, 1:13 PM
Not really helpful, but I am planning on adding support for such Swift to Kotlin cases.
m

Marc Reichelt

02/22/2023, 1:25 PM
Thank you for answering - and thank you for providing this library, which is really useful to us! 👏 🎉 I saw the issue you referenced, but thought it would be a different problem 🙂
r

Rick Clephas

02/22/2023, 1:31 PM
The whole issue is somewhat more complicated, but it mainly consists of 3 parts: • adding support for
Flow
arguments • adding support for
suspend
arguments • adding support for Swift implementations of coroutine declarations (e.g. subclassing a Kotlin class in Swift and overriding a suspend or Flow declaration) The first two aren’t that complex, the later causes more headache 😅.
m

Marc Reichelt

02/22/2023, 1:59 PM
Yeah, it sounded a bit more complicated - that’s why I was asking 🙂
We’ll try to implement something ourselves, maybe we can just use
MutableSharedFlow
for example
I just wondered how others do it - because this seems to be a problem which more people should run into. Maybe most people just add a parameter like
onValueChanged: (Boolean) -> Unit
and call it a day 😄
r

Rick Clephas

02/22/2023, 2:06 PM
Yeah depending on the use case that might actually work. I guess it really depends on the architecture if you need something like this. E.g. if you expose ViewModels you are less likely to have to expose a
Flow
back to Kotlin.
m

Marc Reichelt

02/22/2023, 4:41 PM
TL;DR: So we tried it out a bit, and it’s possible to make this work. The tricky bit is where to store the subscription of Combine somewhere, along with the type conversions for KotlinInt, KotlinBoolean etc. We stopped investigating this for now. Hopefully we’ll have something to share sometime in the future.