In this article we will discuss about various RxJava create operators in depth with examples. Here we will discuss how to create Observables using various operators like Create, From, Just, Differ, etc.
This article is part of RxJava Intro series. You can checkout the entire series here:
- RxAndroid Introduction
- RxAndroid Example – RxJava Tutorial #2
- RxJava Operators In General – RxJava Tutorial #3
- RxJava Operators for Creating Observables – RxJava Tutorial #4 – You are Here
- RxJava Operators for Transforming Observables – RxJava Tutorial #5
- RxJava Operators for Filtering Observables – RxJava Tutorial #6 – Coming Up
- RxJava Operators for Combining Observables – RxJava Tutorial #7 – Coming Up
Let us explore these RxJava create operators one by one.
RxJava Create Operators
Table of Contents
Create operator helps creating an Observable from scratch by calling observer methods programmatically. It creates only one Observable for all Observers.
We have already seen an example on how to create an Observable using Create Operator. Let us re-collect the same by creating one more Observable.
In the above example, you can identify that in Line 48, we created an Observable using Create Operator. If we dive into RxJava documentation link for create() we observe that it does not take any values. Hence we declared the values as list and iterated through them using onNext() function in line 49 – 50.
Here is the output:
Defer differs from create in a way that Defer does not create an Observable unless and until an Observer is subscribed to it. It waits until an Observer subscribes and generate a Fresh Observable with fresh data. So even though the Observer thinks it is subscribing to Same Observable, it gets a Fresh Observable.
As this Operator waits till last minute to create new Observable, this ensures that the Observable always contains fresh Data. So this Operator is mostly used when intended to fetch freshest Data
Here, I’ve created an Observable using Defer Operator as shown in the below example.
In the above example, we created a TestClass by initializing a variable named str to “Hello”. I’ve taken two types of observable – one is with Just operator and other is with Defer operator.
In line 24, I’ve modified the str value to “World”. In lines 25, 30 we subscribed to both observables. Now If we see the logcat, the output is printed as follows:
We can clearly see that Defer operator is fetching the latest data by creating a fresh Observable.
From operator is used to transform objects like lists, arrays, futures etc into Observables.
There are different methods for transforming different objects like fromArray(), fromCallable(), fromFuture(), fromIterable(), fromPublisher(). More info is provided at the RxJava Wiki Link.
Here I will consider an example using fromIterable where I will be converting arraylist into Observables.
In Line 22, I’ve transformed the numList to Observable.
This operator creates an Observable that emits a sequence of Integers with a given particular interval of time.
Below is the example where I used Interval Operator to create Observable and print Integer starting from 0.
In line 20, I created a clock Observable using Interval Operator and given it an interval of 1 second. Now the output for this above will be as follows:
This will print until the activity is destroyed as we are disposing disposable. Be careful that if we don’t dispose the disposable, it will continue to printing the values even if we exit the activity.
Just Operator converts an item into an Observable that emits that item. Here whatever we pass in as arguments, it is converted to an Observable and will be emitted. The max arguments that can be passed in Just Operator are 1 to 10 (so min of 1 argument and max of 10).
Here is the first example of creation of Observable using Just Operator where I passed the numbers 1, 2, 3, 4 directly as arguments.
The Output for the above is :
We can also pass List as a parameter.
The output will be as follows:
Range Operator creates an Observable that emits a particular range of Integers. It takes 2 arguments – starting integer n and length m, so that it starts emitting integers from the starting integer n to n+m-1.
Here is the example where we created an Observable using Range Operator.
The output of the above example is:
Repeat Operator is a simple operator which emits the same item multiple times
Let us see its implementation with example.
The output for the above is:
Timer operator creates an Observable that emits a particular item after a given delay.
Here is the example of timer operator:
The output for the above is :
This completes our tutorial on RxJava create Operators which we use to create Observables. In the next tutorial, we will focus on RxJava’s Operators for Transforming Observables along with examples.
All the code for the above examples can be found in the following Github link. Hope this article is useful to you. Make sure you subscribe for the mail to notify about the latest articles first. Let me know if any queries through comment section.