Rx Android
Aalishan Ansari
SDE-2 @TESCO | Android | Kotlin | Java | MVVM | Coroutines | Rx Android | Architecture components | Jetpack Compose | Product base | Startups | Individual & Open Source Contributor | UI/UX Designer | Android Blogger
Rx Android is a concept used in android app development to give app better performance.
Reactive Extension?is a library that follows Reactive Programming principles to compose asynchronous and event-based programs by using the observable sequence.
Rx stands for Reactive programming, In android, it can achieve by using Rx Java and kotlin.
RxAndroid?is specific to the Android platform which utilizes some classes on top of the RxJava library.
When to use Rx Android
1.There are many situations where we can use Rx Android but choose wisely
2. Rx android can be used when you want to listen to some stream.
3. The continuous data source you want to get data from.
4. Data inside data kind of API
5. You want to perform some operation or modification on data before using it.
Rx android has mainly 4 Components
Once?Observer ?successfully subscribes to?Observables?it will get a callback in the following methods.
If any Observer has subscribed to Observable it will start emitting data.
Types of Observables
Disposable and CompositeDisposable
Disposable(Represents a disposable resource.) is an interface which is responsible for Free up the subscription or we can say which is responsible for de-link the listeners from listing the events
Let me elaborate on this Suppose I am listening something on one screen and at some point, I go to another screen so does it make sense to listen data in the previous screen obviously no so we need to dispose of the listeners or in simple word we need to unsubscribe our listeners from listening,So that unused resource can be garbage collected and we can avoid memory leak.
Disposable is having 2 methods
void dispose(); Used to dispose the subscriptio
boolean isDisposed(); Check the status
CompositeDisposable?is a class that implements Disposable.
A disposable container that can hold onto multiple other disposables and offers O(1) add and removal complexity.
Observable uses some?operators?for emitting data so that we can modify data according to our needs. for that, we are already having some built-in?Operators
Some important?Operators?in Rx Android
Create
This operator creates an Observable from scratch by calling observer methods programmatically. An emitter is provided through which we can call the respective interface methods when needed.
The?create()?the method does not have an option to pass values. So we have to create the list beforehand and perform operations on the list inside the?onNext()?method.
Defer
This operator does not create the?Observable?until the?Observer?subscribes. The only downside to?defer()?is that it creates a new?Observable?each time you get a new?Observer.?create()?can use the same function for each subscriber, so it’s more efficient.
领英推荐
Interval
This operator creates an Observable that emits a sequence of integers spaced by a particular time interval.
Just
This operator takes a list of arguments (maximum?10) and converts the items into Observable items.?just()?makes only 1 emission. For instance, If an array is passed as a parameter to the?just()?method, the array is emitted as a single item instead of individual numbers. Note that if you pass?null?to?just(), it will return an Observable that?emits?null?as an item.
Form
This operator creates an Observable from a set of items using an Iterable, which means we can pass a list or an array of items to the Observable and each item is emitted one at a time.
Difference between?Observable.from()?and?Observable.just()?— For the same inputObservable.just()?emits only once whereas?Observable.from()emits?n?times.
Range
This operator creates an Observable that emits a range of sequential integers. The function takes two arguments: the starting number and length.
Observable.range(2, 5)
Repeat
This operator creates an Observable that emits a particular item or sequence of items repeatedly. There is an option to pass the number of repetitions that can take place as well.
Timer
This operator creates an Observable that emits one particular item after a span of time that you specify.
Observable.timer(1, TimeUnit.SECONDS)
Difference between?Observable.interval()?and?Observable.timer()?—?timer()?emits just a single item after a delay whereas?interval()?operator, on the other hand, will emit items spaced out with a given interval.
If you want to modify data emitted so you can use the following Operators
Buffer
This operator periodically gathers items from an Observable into bundles and emits these bundles rather than emitting the items one at a time.
Observable.just(“A”, “B”, “C”, “D”, “E”, “F”) .buffer(2)
Will emit data in pair of 2.
Map
This operator transforms the items emitted by an Observable by applying a function to each item.?map()?the operator allows for us to modify the emitted item from the Observable and then emits the modified item.
Map?operator can be used when we fetch items from the server and need to modify it before emitting to the UI.
FlateMap
This operator transforms each item emitted by an Observable but instead of returning the modified item, it?returns the Observable itself?which can emit data again. In other words, they merge items emitted by multiple Observables and returns a single Observable. The important difference between FlatMap and other transformation operators is that the order in which the items are emitted is not maintained.
FlatMap?operator can be used when we know that the order of the items are not important.
SwitchMap
Whenever a new item is emitted by the Observable, it will unsubscribe to the Observable that was generated from the previously emitted item and begin only mirroring the current one. In other words, it returns the latest Observable and emits the items from it.
SwitchMap?is best suited for scenarios such as a feed page when pull to refresh is enabled. When the user refreshes the screen, the older feed response is ignored and only the latest request results are emitted to the UI when using a SwitchMap.
ConcatMap
This operator functions the same way as?flatMap(), the difference being in?concatMap()?the order in which items are emitted is maintained. One disadvantage of?concatMap()?is that it waits for each observable to finish all the work until the next one is processed.
Conclusion
It's good to use Rx Android in your projects but be sure about requirements and choose wisely Operators.
Because bad use of RX can lead you to end up with memory leak.
Android Developer at Entro Labs IT Solutions Pvt Ltd
9 个月can u send u r email id