RxJava for 100% beginners(part 2-write your code in a more elegant way)
In the last section I introduced some basics from functional programming, in this section we are gonna go through the real deals! RxJava!
There is a very nice example which I wanna borrow from, if anyone of you can read chinese, you are encouraged to read the original here:
我从去年开始使用 RxJava ，到现在一年多了。今年加入了 Flipboard 后，看到 Flipboard 的 Android 项目也在使用 RxJava ，并且使用的场景越来越多 。而最近这几个月，我也发现国内越来越多的人开始提…gank.io
if not, i think i can just copy paste the example:
in the above example, we are trying to :
- traverse a list of folder
- traverse every file from the folder list
- load the files which are “png” format and construct them as Bitmap
- load the bimap to a “View”(do it in the UI thread to avoid exception)
noted that except for step 3, every step else will be executed in a new Thread other than UI thread.
Is this example complicated? Well that depends, but one thing Im almost sure is that : These few lines of code is hard for expanding its functionalities. Lets say if we want to just take the first 10 “png” file(I guess we have to use a global variable as a counter right, which breaks the rule of functional programming XD) ? or if we want to traverse the files in Thread A, and then process the files to images in Thread B(most naive way will be using a new Thread().start(), but the code will be nasty)?
Just Imagine how complicated your code will be after the expansion in a normal way. And even if I believe you are a great software engineer, you manage to solve it, you teammates who are working on the same project will be like:”What the hell is these” when they pull your code change…..
so lets check out how RxJava can handle it better!
if we want to achieve the those extra functionalities above ->
1.take only the first 10 element. 2.switch threads between traversing files and loading bitmap
we just have to add few more “operators” (which is a term from RxJava, it refers to the way we wanna change the current stream).
I believe that there are a tons of “operators” which you are not familiar with . But thats totally fine, I will go through them one by one in the following sections, right now all you need to do is to admit that the code is much much cleaner and more readable.
So By using RxJava,
- we flatten our code in a very elegant way, code pyramid of doom is no longer there!
2. switching threads for different operations are easier than before!
So before we start to dive into those “operators”, there’s some conceptsI want you to keep in mind.
- RxJava, or general functional programming may not be a ideal tool to boost up your performance(if you have any performance issue in your app).Its just a new programming paradigm which helps you to have a cleaner and more readable code, as well as facilitates your abstract thinking and bring them to code without caring too much on the implementation details(like how to traverse a collection, how to change thread).
- In RxJava, every time we use a “Operator” to operate on an “Observable” (which is the “event stream” we keep talking about), we manage to get a new “Observable”. For beginners , I don’t encourage you to go too deep into how RxJava implement this “feature”, you just have to know that the “event stream” will be changed to a new one, everything else is built on top of this, thats all you have to care about.
So thats it for section2, lets cover some most popular operators from RxJava in the next section!