Unsubscribe Me, Please

Do you know this feeling? I always feel great unsubscribing from an email which keeps coming to my Inbox. I feel great ‘unsubscribing’ from a couple of kilos loosing some weight. I experience similar feelings unsubscribing from Rx operations.

I dare to remind you that every time you subscribe to an Observable (or if you’re really cool, to Single or even Completable) you get a Subscription back. It’s nice to have one as no matter how much you enjoy what you see on the screen you should carry on and don’t look back. Whenever one of your Rx operations are still ongoing leaving the screen, the memory leak preventing the former from garbage collection will make you not only to look but actually go back and fix the problem.

fun onThatCoolButtonClicked() {
val subscription: Subscription =
Observable.fromCallable { doLengthyNetworkOperation() }()
.subscribe(/* bla bla bla */)

That’s why tomorrow’s version of you is gonna thank today’s one if you do leaving the screen:


Usually, after I’m done with my next hello word app I have more than one Subscription in a .. cough.. Presenter (or whatever). And I’d rather unsubscribe from all of them.

Fortunately, these smart people behind the library devised such a great things as CompositeSubscription which you gonna like as instead of:


you could gather them all under the cozy CompositeSubscription’s hood:

val subscriptions = CompositeSubscription()
subscriptions.add(observableOne().subscribe(/* ... */))
subscriptions.add(observableTwo().subscribe(/* ... */))
subscriptions.add(observableThree().subscribe(/* ... */))

and unsubscribe in one go gaining a decent portion of dopamine at a cost of only one line:


or even better:


which not only does unsubscribe you from all subscriptions you collected but make it able to use subscriptions again which will come in handy in one of your lovely Adapters.

By the way, if you are using Activities and Fragments, you might want to check RxLifecycle

But I digressed.

Being a good scout having an appropriate amount of unit tests I don’t make myself wait long until refactoring the code so that tomorrow’s version of me is grateful again, this time for a simplified daily activity such as reading the code.

Alas, that great CompositeSubscription fellow doesn’t add a great deal to readability:

.subscribeOn(/* ... */)
.observeOn(/* ... */)
.subscribe(/* ... bla ... */))

Not that bad you may object. Maybe! When your Observable isn’t so lean as this pseudo guy, when you apply all the power of operators in between it and the Subscriber, having one additional level of indentation is not cool.

So exclusively for satisfactory reasons you could use a tiny Extensions

fun Subscription.addTo(subscriber: Subscriber) =  

and keep your code a little bit nicer:

.subscribeOn(/* ... */)
.observeOn(/* ... */)
.subscribe(/* ... */)


When was the last time you checked your pet project? It must be screaming for at least partial conversion into Kotlin, levels of domapine are gonna be even higher.

Love to Java and happy coding.