Grokking Android

Getting Down to the Nitty Gritty of Android Development

Why use Observable.create() and not just inherit from Observable?

By Leave a Comment

When starting to use RxJava you have to create Observables. They are at the very core of RxJava. But how to do so?

A look at the Observable class might make you dizzy. Looking at the source even more so. Not only does this beast consist of nearly 10.000 lines (though, 7600 lines of that are comments) but it also consists of a bunch of final methods. Actually only final methods! 330 of them! But you can inherit from Observable. Odd, very odd!

Next you might think: Ah never mind, let me just inherit from Observable and see how far I get.

But should you bother to look at the documentation of the constructor, you see this:


Note: Use create(OnSubscribe) to create an Observable, instead of this constructor,
unless you specifically have a need for inheritance.

Okay, since you are eager to know what this is all about: you have a short look at the documentation of onCreate():


Returns an Observable that will execute the specified function when a Subscriber subscribes to
it.
...
Write the function you pass to create so that it behaves as an Observable: It should invoke the
Subscriber's onNext, onError, and onCompleted methods appropriately.
...
A well-formed Observable must invoke either the Subscriber's onCompleted method exactly once or
its onError method exactly once.

Hm... Maybe a look at this method’s code might help?


public final static  Observable create(OnSubscribe f) {
   return new Observable(hook.onCreate(f));
}

What? It's just passing this stuff to the constructor anyway? So what's it with this warning?

Well, first: Why would you want to inherit Observable in the first place? All methods of it are final. You basically cannot add much functionality to Observable by inheritance because of this. It's better to stick to the RxJava way to do things: And that is by chaining API calls of its fluent API.

Another thing is, that it's not as explicit. Using the create() method you directly see what the created Observable is up to.

Furthermore: If you look more closely at the source snippet, you will notice this tiny little hook.onCreate() call. That's actually quite important. Because RxJava allows you to supply hooks that wrap certain method calls and allow you to substitute certain aspects of how RxJava behaves.

I for one use this hook in debug mode to log which thread my observables get created on and on what threads operators are lifted. In server environments you might want to add monitoring logic to your hooks. By using the constructor, you bypass this and rob yourself of the possibility to do so easily.

Having said all this: It's not forbidden to inherit from Observable. As with all rules, there are situations where they do not apply and where inheritance does make sense. The Subject class for example does inherit from Observable. Just be careful and try to think twice when doing so  đŸ™‚

Wolfram Rittmeyer lives in Germany and has been developing with Java for many years.

He has been interested in Android for quite a while and has been blogging about all kind of topics around Android.

You can find him on Google+ and Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *