Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
RxJava contains built in Operators and Schedulers. Operators can do more advanced things like combine two pieces of data into a single item to be emitted. While Schedulers can be useful for performing long lived calls.
Resources:
RxMarbles: Interactive Diagram of RxObservables
Treehouse Course: Threads and Services
-
0:00
As data is emitted from an observable, we can optionally add steps to manipulate
-
0:05
the data before it reaches the observer's onNext method.
-
0:09
Manipulating data is done using operators.
-
0:12
Operators act on data from an observable before it reaches the observer.
-
0:17
RxJava contains many built in operators such as filter [SOUND], map [SOUND], or
-
0:22
skip [SOUND].
-
0:23
Operators can also do more advanced things,
-
0:26
like combine two pieces of data into a single item to be emitted.
-
0:31
A helpful way to understand these operators is to visit RxMarbles.
-
0:35
This site provides interactive diagrams for common RxJava operators.
-
0:40
I've added the link in the teacher's notes.
-
0:43
Let's take a look at a couple to understand these diagrams.
-
0:47
Okay, open your browser and go to rxmarbles.com.
-
0:52
You can see here, there's a diagram in the middle and
-
0:54
there's a bunch of operators listed on the left hand side here.
-
0:58
Let's just go through a couple to see how it works.
-
1:02
We choose, first.
-
1:04
Here we can see the diagram, I can change the data being emitted by the observable.
-
1:09
Just like our diagram before, the observable is on the top,
-
1:13
the observer is on the bottom.
-
1:15
So here we can see that as the number 1 gets emitted,
-
1:19
it gets observed by the observer.
-
1:23
But also notice that as 2, 3, and 4 get emitted,
-
1:26
only the first item is still passed to the observer.
-
1:30
Let's go ahead and change one of these items being emitted.
-
1:32
Let's pass in 2 as the first item.
-
1:35
So if 2 is the first item emitted by the observable,
-
1:38
we can see that 2 is then the only item received by the observer.
-
1:44
So that's what first does, only emits the first item.
-
1:49
Okay, let's take a look at another one here.
-
1:50
How about map?
-
1:54
Here we can see this map is a function that takes in a value x and
-
1:58
multiplies it by 10.
-
2:00
So that means whatever value is emitted by the observable,
-
2:03
will be multiplied by 10 and that's the value the observer will see.
-
2:08
Here we can see that 1 is emitted and 10 is received, 2 is emitted, 20 is received.
-
2:15
We can change the order of these, it doesn't matter.
-
2:18
Map doesn't change the order, it simply adds a function
-
2:23
as an operator to the value and passes that result to the observer.
-
2:28
Let's go in and take another one.
-
2:30
How about merge.
-
2:31
So what does merge do?
-
2:34
Well, we can see that merge has two observables here, so
-
2:38
there's one observable on the top.
-
2:40
It's emitting 20, 40, 60, 80, 100.
-
2:43
And then we have another observable emitting 1 and 1.
-
2:48
Then we have an operator merge.
-
2:50
And then we have what the observer sees.
-
2:54
So if you take a look at this,
-
2:54
the observer sees the items from observable one up here on the top.
-
2:59
It also sees the items from the second observable.
-
3:02
And that's what merge does.
-
3:04
It takes the items from multiple observables, and
-
3:06
then emits them all to the observer.
-
3:09
So we can see this observer received the items from the first observable and
-
3:12
the second one as well.
-
3:14
We can change the order here and you can see that the order remains the same.
-
3:21
Whichever order they are emitted from either observable,
-
3:24
the observer sees them in that order as well.
-
3:28
Okay, and let's look at another one here.
-
3:30
How about combineLatest?
-
3:32
What does that do?
-
3:34
combineLatest here is a function, it takes in two values and
-
3:38
it concatenates them together.
-
3:41
So we can see we have again multiple observables,
-
3:44
the top observable emits numbers 1, 2, 3, 4, 5.
-
3:48
A second observable emits letters A, B, C, D.
-
3:53
So if a 1 and then an A comes along, and
-
3:56
then combineLatest takes the 1 and the A and it concatenates them and
-
4:01
it makes 1A, and that's what the observer sees.
-
4:06
So notice the observer doesn't get an item until there isn't two items emitted,
-
4:12
one from each observable.
-
4:13
Because the operator has to combine items, one from each observable.
-
4:17
So it needs to have an at least one item from each.
-
4:20
Notice that the next item emitted is 2A.
-
4:23
Why is that?
-
4:24
That's because 1's been emitted, then we have an A, then we have a 2.
-
4:30
And so it takes the latest two items.
-
4:33
That's gonna be the 2 and the A before it, cuz the B hasn't come yet.
-
4:36
Once the B comes then it will be 2B.
-
4:39
And if we change the order of these, you can see it makes sense,
-
4:43
it also changes the order and it changes what the observer sees.
-
4:48
Once the 1 and the B is emitted, we get 1B.
-
4:52
Then the 2 and the B are together combined to receive 2B in the observer.
-
4:59
Then once the A comes, the 2 and the A are combined together to receive 2A, and so
-
5:03
on and so forth, all of these.
-
5:04
And you can move these around and see how it changes what the observer sees by
-
5:08
combining the latest two values from two different observables.
-
5:13
Up until this point we haven't discussed threading.
-
5:16
As you know, threading is an important topic on Android to maintain a fluid and
-
5:21
fast application.
-
5:22
And there's a course listed in the teacher's notes if you wanna learn more.
-
5:26
However, handling multithreading has also been a major pain point for developers.
-
5:32
RxJava helps with this by providing a clean API to handle multithreading
-
5:36
called a scheduler.
-
5:38
A scheduler isn't a thread, but it describes a threading model underneath it.
-
5:43
This means RxJava is flexible to whatever type of threading model you are using.
-
5:49
By default, observables will emit items and
-
5:52
they will be passed to the subscriber on the thread the subscription is created on.
-
5:56
However, if you wanna change which thread items are received on,
-
6:00
you can use the method, observe on.
-
6:02
This ensures the calls to the observers on next., on completed and
-
6:06
on error are called on whatever scheduler you provide.
-
6:11
On Android, we need to do UI work on the main UI thread.
-
6:15
That's why the Rx Android companion library contains
-
6:18
the AndroidScheduler.mainThread class.
-
6:22
Using this scheduler ensures your observer receives OnNext on the main UI thread.
-
6:26
RxJava includes other scheduler's by default,
-
6:31
such as, schedulers.IO which is useful for performing long live calls.
-
6:37
So how would you move your long work off the UI thread?
-
6:40
The subscriber on operator specifies a scheduler on which the observable
-
6:44
should operate.
-
6:45
It's important to note that subscribe on can be called in
-
6:48
any order of your chain of operators.
-
6:51
The last call to subscribe on will determine which scheduler is used for
-
6:55
that observable.
-
6:57
Now that we've gone through the basics of the RxJava API, let's dive into an app,
-
7:02
and convert it to use what we just learned.
You need to sign up for Treehouse in order to download course files.
Sign up