I've a kotlin interface that contains Flows. Exam...
# multiplatform
d
I've a kotlin interface that contains Flows. Example:
Copy code
interface MyInterface {
  fun stream(): Flow<MyObject>
}
I'm using SKIE and while they automatically convert Flows into AsyncSequences they say they do not support the conversion from AsyncSequences to Flow. Can I still implement such conversion myself in the iosMain target? Do you have any references? Thank you in advance
Anyone? Was this the wrong channel to ask in?
p
Perhaps in #CTJB58X7X
a
Usually you convert flows to asyncSequence to consume it in swift
d
But I'm not asking about SKIE. I'm asking how to write a converter from AsyncSequences to Flow inside the iosMain target of a multiplatform project. I already know this isn't supported directly by SKIE. And this isn't a feature request
a
Why would you need it the other way?
d
Yes, but in this case I've some interfaces in kotlin that return flows and I'd like to implement them either on the swift side or the iosMain side
The swift part of the code needs to produce the stream, the kotlin part of the code will use it
a
And where do you subscribe to the sequence produced in swift and consimed in kotlin?
d
Inside kotlin code.
a
Yeah but its a controller, ui , service?
d
We have android and iOS authentication in place and we want to start a partial migration to ksp by writing some new features in common. The authentication part will be kept native for now. However the kotlin parts needs to be able to know if the user is currently authenticated in several parts. The store for credentials token emit a flow which is used in many different parts of the code. It's not the only place where we need this. But it is the most important one. Are you asking all of this because it isn't possible?
@Alexandru Caraus anyway, it's a lower level component that will have to be injected by the swift part when initializing with DI and will be used by multiple places including domain use cases and UI controllers.
r
In principle it's possible, though I'm not aware of anything publicly doing it right now. I had some demo code a long time ago for a client but I don't think it still exists. Broadly, what you need is to create a callback interface that you can use to cross the language boundary. In Swift, you consume your AsyncSequence and convert it to onNext and onError callbacks. In Kotlin, you create a function that takes those callbacks as input and returns a Flow. Then you can get fancier by adding onCancel so that you get cancellation across languages.
This might nerd-snipe me into building a demo but no promises
d
I was thinking of doing something similar, but since I couldn't find anything like that online I started to second guess myself thinking it was trickier than that. So I came here.
a
It might be possible, but I still dont understand why would you need this, probably missing some context
If you inject a kotlin flow in swift, you can transform it to asyncSequence from touchlab or kotlin native coroutines libs, and consume it there
In android you consume it the usual way
In vm or service
d
2 ways interoperability some part developed in the iOS swift side needs to talk with the kotlin world and the kotlin world uses Flow for streams. Therefore we need a way to convert a swift stream into a kotlin stream.
The stream is to be created by iOS in this case.
a
Swift talks to kotlin usually with suspend functions
Sends user generated events
As function invokation
Not as stream
You can do a class in kotlin that has a function to emit an element and that element to a hot stream, and then subscribe to that stream wherever you need in kotlin
d
On android we used databuff + datastore for storage. Data was exposed with stream. Since some part was already implemented in the iOS side and we do not intend to migrate it to KMP we need to make those interoperable. The easiest way to do so is to have iOS implement the same interface that we use in kotlin for the storage. Therefore we'd like to know how other developers converts iOS / swift streams to Flows. That's it.
I also don't want an hot stream
a
Okay, hope you find a solution, would be nice to see it. Why hot stream "bad"?
d
Because everything else is built around the assumption that's a cold stream
a
And if one of the streams is hot then what could go wrong with that?
d
In this particular case I expect to only go read the database when it's needed, I also want to be sure that the first read is always fresh from the DB. A hot stream could be replaying the last event or not. It can work with a hot stream. But I'd rather not implement the system with that in mind.