Hi everyone, I am a Kotlin Flow learner. I am wo...
# android
l
Hi everyone, I am a Kotlin Flow learner. I am wondering if there is a way to get a ViewModel instance observing the state change in a Flow instance, for instance a new value is added in and ready to be collected? I am felling I might misunderstand the concept of the Flow or miss some key information. Could anyone help me? Thanks in advance!
d
Look at StateFlow
l
Thanks @dan.the.man, it is working. Is there any of special Flow classes for receiving network notification messages?
d
No, I'm not super clear on what exactly you're asking, but Flow has no relation to anything networking related. Presumably you'd put some kind of object that would have that, various libraries have a HttpResponse type object which would contain w/e you'd want
l
Thanks for your reply. I am facing a practical problem, the incoming data is from a server over a data channel after the app subscribes the notification from the server. Unlike pull request/response, it pushes the data to the Android app. One possible solution I suppose to put the data into a blocking queue, later on other threads are scheduled to consume it. Considering Kotlin Coroutines and Flows, seems there may be a better way to structure the concurrency. In my case, ideally, the data even could be consumed by multiple observers not just be passed onto UI.
z
Right now, the best solution for what you’re looking for might be
BroadcastChannel
, and exposing that as a
Flow
. When coroutines 1.4 is released, you’ll probably want to use
SharedFlow
.
l
That’s is really good, thanks @Zach Klippenstein (he/him) [MOD]