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