Hey! It’s been a while since I wrote the previous post, but as I said these have been busy days so I guess that’s my excuse.
Last night I worked again on the app, I did something I’ve been thinking of for the last couple of weeks: I’ve moved from AsyncTask to RxJava!
Well, it’s not that for the project I need it (for now, there’re not many asynchronous events, API calls, etc.) but I think it’s something cool to learn and probably something useful, since for more complex apps it seems a really good solution.
So when I was in Droidcon Italy, I had a chat with Jamie and Mathias from SoundCloud about RxJava and how awesome it is, so I decided to try myself: I converted the Renderscripting demo into RxJava, result here and I think it went pretty well!
So first I had to read some resources from the Internet about RxJava and Reactive Programming. I found it hard to understand in the beginning and also, IMO the namings used (Observable, Observer, Subscription, etc.) are a bit confusing when you’re not used to them. I’m still not familiar enough to explain myself in detail how RP works with RxJava, but there’s a bunch of information on the Internet about it (see the list of links at the end of the post).
I guess if you’re familiar with Functional Programming, the learning curve should be flatter, which in my case doesn’t really apply (although I started learning FP, I never spent enough time so it’s still in my To Do list).
Show me teh codez
What I did is still not completed and it’s not stable enough, so I created a branch for it and I’ll keep working on it until I think it’s as stable as it’s now in the master branch. You can check the code here and see the diff with master here.
I think one interesting point here is that I almost didn’t need to modify any Task-related class – in fact, it got simplified in the app module, since we don’t need to do funky stuff because of an
Another good point is that the Tasks are still testable, since the only thing I had to do is build the
Observables around them, which means, you can run a Task synchronously or just get an Observable that executes that task. I think you can even execute that Observable in the current Thread if you wish to test it! This means Tasks can still be simple, for example:
This is how the
SummaryTask (the task that gets a
Summary from a post) looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
And so my
TaskFactory just needs to create an
Observable that executes this task:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Simple, isn’t it? Last step is execute it and create an
Observer that will get the output sequence from this Observable (which in this case is just one item) and update the UI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
That’s it! Thanks to the RxJava-Android library we can use the
AndroidSchedulers.mainThread() Scheduler in order to get the callbacks in the UI Thread :)
A decision I took is not to use Retrofit’s built in RxJava support, since I don’t feel there’s a need for it at this pont. I want to learn how to use RxJava properly, so I want to write all the bits myself as much as I can. This doesn’t mean I don’t like the idea of having it already built in Retrofit, I’ll probably use it in the future.
As I said, this is still far from perfect. The biggest issue is orientation changes. There’s no built in support for that, but I guess the
cache Observable operator can be used for caching and emiting the sequence of results after rotation.
Also testing!! I really want to spend some time writing tests. It also would be nice to set up a Jenkins server and perform some code analysis (PMD, Findbugs, Checkstyle and Lint), I’ll try to spend some time this weekend on this – will share if I do!
A smaller issue is there must be a way to compose the two
Observables I use in the
NewsDetailsFragment (the one above + the one that parses the Markdown), I just need to sit and spend some time reading the docs, so I consider it a minor issue. But apart from that, the rest looks good to me.
I want to keep learning how to do things ‘the Reactive way’. I know I’m probably doing things wrong and I’ll learn from my mistakes, in fact that’s why I do this in my side project, I don’t mind if I have to spend time rewriting the same code over and over again :P
I have to say though, the first time I tried to do something with RxJava I felt like “OMG WHAT IS THIS?”, which is good because it reminds me that there’s a bunch of stuff I want to learn. That feeling when you do something new for the first time and you’ve got no idea and then you start feeling a bit more confident as you keep working on it, makes you feel so good!
That being said, I’ll go to bed since I’m really tired and I’m probably writing senseless sentences.
- RxJava project by Netflix
- Introduction to RxJava for Android
- Using RxJava in Android
- Slides about RxJava in Android
- The branch I’m implementing RxJava stuff
If you want to discuss about RxJava, write your comment below!