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