Johnjake Talledo
08/21/2021, 9:39 AMSharedFlows
shareIn
in github or gitlab, I couldn't find good resources or articles with a sample. What I am trying to achieve is combine(flow1, flow2) with shareIn
, so I only need to getfetch
once and share this to at least 3 fragments. I really appreciate if someone could help. Best Regards to all.Alexandre Brown
08/21/2021, 4:54 PMshareIn
or `stateIn`(it's the same principle)
class MyClass(override val coroutineContext: CoroutineContext = Dispatchers.Default) : CoroutineScope {
private val myPresentation: SharedFlow<Presentation> = combine(
myRepository1.myStateFlow,
myRepository2.myStateFlow,
myRepository3.myStateFlow
) { a, b, c ->
myPresenter.present(a,b,c)
}.shareIn(this, SharingStarted.Lazily)
...
fun getAbc() = myPresentation
}
To get a StateFlow instead you would simply change the code to myPresentation.stateIn(this, SharingStarted.Lazily, presentDefaultValue())
Depending on what you want you might want to do shareIn
only once in a property(1 instance) or in the function call (1 instance per call). In most cases having 1 instance is probably what you want but make sure it works with any 3rd party extension like asLiveData
etc.ursus
08/22/2021, 11:16 AMJohnjake Talledo
08/22/2021, 12:34 PMursus
08/22/2021, 12:36 PMJohnjake Talledo
08/22/2021, 12:40 PMAlexandre Brown
08/22/2021, 12:40 PMshareIn
at the end of the property and return that property in the function call (see the updated code)Johnjake Talledo
08/22/2021, 12:43 PMmyPresenter.present(a,b,c)
do? Is this where you implement eg: asLiveData
in my case I use MutableSharedFlow
?Alexandre Brown
08/23/2021, 9:46 AMmyPresenter.present
is just hypothetical code that represents some transform. Usually we use combine to listen to multiple flows and when we want to receive updates even if not all the flow changed. Then inside the lambda of the combine we'd use the various lambda parameters to perform some transformation (eg: Create a data class with some info from the various flows values). If you don't want to merge multiple flows into 1 result and be notified of each updates in a single place then perhaps you don't need combine at all. In your case it's much simpler.
You can simply do
private val deliveryUpdates: SharedFlow = repository.getDeliverable(token, "id", "name,count,...").shareIn(this, SharingStarted.Lazily)
Same principle for the restaurentUpdates
Johnjake Talledo
08/24/2021, 1:36 AMshareIn
or Hot flows
when the app goes background does it still continue to emit if the end point has a new item or value?ursus
08/24/2021, 9:25 AMAlexandre Brown
08/24/2021, 11:28 AMJohnjake Talledo
08/24/2021, 12:53 PMgetDeliverables
and getRestuarant
as a test. I was more familiar with zip
in rxJava
and combine
is equivalent of zip
and when I read about ShareIn
this probably solve my problem, what I wanted to achieve is to continue the upstream
to fetch data from api, then collect this in other fragment, so I will not be calling the same expensive network calls because it takes sometimes 17 to 25 milliseconds before data on recycleview will show up.