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
Use `CompositeSubscription` to manage all subscriptions.
-
0:00
On Android it's common that during rotations we need to release
-
0:03
any resources we have, and allow our activity to be rebuilt by the system.
-
0:08
This means we need to unsubscribe to all these subscriptions that we've set up.
-
0:12
So here we have our combined latest subscription, and
-
0:16
we also had our clicks for adding new items both of those subscriptions.
-
0:20
We need to unsubscribe to those during on destroy, and
-
0:24
there's a common pattern for doing this, and it's called composite subscription.
-
0:29
So let's go ahead and go up to the top here, and what we're going
-
0:34
to do is we're going to add in a new type called composite subscription.
-
0:44
We're gonna call it subscriptions and create a new composite subscription.
-
0:52
So what this is gonna do is this is going to allow us to merge together or
-
0:57
handle and keep track of all our subscriptions in a single place.
-
1:02
So it's just delegates to all subscriptions we add to it.
-
1:07
So we're gonna do now is add to our current subscriptions to this
-
1:13
composite subscription.
-
1:14
So let's go down here, and where we did our combine latest.
-
1:21
Let's go ahead and do, subscriptions.add,
-
1:24
and add in this entire subscription.
-
1:33
And let's go ahead and
-
1:37
go back up to our clicks, and
-
1:43
go ahead and add this one as well.
-
2:01
Okay, so what we can see is that we added our subscriptions for
-
2:05
our observables to this composite subscription, here and here.
-
2:10
And then what we're gonna do is down and on destroy,
-
2:14
we'll be able to actually unsubscribe to these all together at the same time.
-
2:19
So it's really easy.
-
2:20
We just go down here, and we say subscriptions.unsubscribe.
-
2:25
And that's it.
You need to sign up for Treehouse in order to download course files.
Sign up