Replace Android AsyncTask with RxJava

Today is the day when we will say goodbye to AsyncTask. It will be replaced by our new friend RxJava which is quite in news now a days. We will be covering how RxJava will do the work of  AsyncTask. This post is included in series of post which will cover RxJava in detail.


 

[su_button url=”https://github.com/androidsrc/RxJavaSample” target=”blank” style=”stroked” background=”#51d461" color=”#ffffff” size=”6" center=”yes” radius=”0" icon=”icon: arrow-circle-o-down”]Download Full Source Code[/su_b[/su_button]p> 

But why ?

That ok but why do we need to replace our AsyncTask? Because RxJava is more easier to use and we can add more features and controls without changing anything and by just plugging some functions in between. This will make our life easier and manageable. Also there will be much less code and no subclassing at all.

Lets start coding

First of all you have to add support for RxJava in your gradle file as explained in this tutorial. We are using lambda expression in this tutorial for code clarity, so you should setup that also from our tutorial.

Now we are ready to use RxJava with Lambda expression.

This code will sleep for 5 sec on worker thread(not on main thread) and then simply print the letter count of passed string. There will be a loader which will show rotating animation from start to end of this operation.

GuRu

Technology enthusiast. Loves to tinker with things. Always trying to create something wonderful using technology. Loves coding for Android, Raspberry pi, Arduino , Opencv and much more.

You may also like...

1 Response

  1. January 18, 2017

    […] Use RxJava instead of AsyncTask Android […]

Leave a Reply

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