![]() StateFlow only returns a value that has been updated. To update state and send it to the flow, assign a new value to the value property of the MutableStateFlow class. The current state value can also be read through its value property. StateFlow is a state-holder observable flow that emits the current and new state updates to its collectors. StateFlow and SharedFlow are Flow APIs that enable flows to optimally emit state updates and emit values to multiple consumers. In other words, shareIn transforms that flow into a SharedFlow, yet the type reflected by the API can be a simple Flow. You can basically re- use that flow and keep emitting items. This will keep the flow alive as long as the attached scope is not canceled. The Flow starts emitting data when collect() is called on the stream, so thatįlows are usually cold! This means, that a flow only emits data when it is collected (or consumed).Ī flow can now also be a hot flow, before collecting a normal flow, you can now call, For example, you can use a flow to receive live updates from a database.įlow can handle streams of values, and transform data in complex multi-threaded ways.Ī Flow object is what Observables / Flowables are in the Rx world, they represent a stream of specific values. In coroutines, a flow is a type that can emit multiple values sequentially, as opposed to suspend functions that return only a single value. Lots of Boiler Plate Codes especially while using Transformations.Lack of seamless data integration across between database and UI especially using Room.Not built on top of Coroutines and Kotlin.Threading issue especially when used in Repositories. ![]() Lack of control over the execution context.Ensures your UI matches your data state.Using LiveData provides the following advantages: If there is a change in data then that data will be reflected to all the observers associated with it but it only notifies the changes to the observers that are live or in the active state and not to that observer that are in the inactive state. If working in some background thread, then you can’t use setValue.they will update the value and notify the observers. If you are working on the main thread, then both setValue and postValue will work in the same manner i.e.setValue and postValue to set the data in LiveData. The MutableLiveData publicly exposes two methods i.e. (No memory leaks) Creating an instance of LiveData, Set the data in LiveData, It is Lifecycle aware- Meaning it sends updates to our UI (Activities/Fragments) only when our view is in the active state. So, instead of having a reference of the activity/fragment in your ViewModel (which you shouldn’t have due to leaks), you now have a reference to the ViewModel in the activity/fragment It is an Observable data class - Meaning it can be observed by other components - most profoundly UI controllers (Activities/Fragments). Live data is part of Android Architecture Components which are basically a collection of libraries that help you design robust, testable, and maintainable apps.
0 Comments
Leave a Reply. |