Hey there! - Using Flows from KMM in Swift is poss...
# multiplatform
m
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:
Copy code
fun doSomething(flow: Flow<Boolean>)
Now we have a publisher like this in Swift:
Copy code
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
Not really helpful, but I am planning on adding support for such Swift to Kotlin cases.
m
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
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
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
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
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.
237 Views