Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of RxJava2 over AsyncTask

Hi I am reading about rxjava2 which is basically for async operations. I found it has an operator concept which AsyncTask in android does not.

How else is rxjava2 different than AsyncTask?

like image 604
N Sharma Avatar asked Oct 23 '17 16:10

N Sharma


People also ask

What is the difference between handler vs AsyncTask vs thread?

AsyncTask are similar, in fact, they make use of Handler , but doesn't run in the UI thread, so it's good for fetching data, for instance fetching web services. Later you can interact with the UI. Thread however can't interact with the UI, provide more "basic" threading and you miss all the abstractions of AsyncTask .

What is difference between thread and AsyncTask?

Thread can be triggered from any thread, main(UI) or background; but AsyncTask must be triggered from main thread. Also on lower API of Android(not sure, maybe API level < 11), one instance of AsyncTask can be executed only once. Long task in general.

Why do we use AsyncTask?

This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field. onPostExecute(Result) , invoked on the UI thread after the background computation finishes.

Why should we use RxJava?

RxJava is a JVM library that uses observable sequences to perform asynchronous and event-based programming. Its primary building blocks are triple O's, which stand for Operator, Observer, and Observables. And we use them to complete asynchronous tasks in our project. It greatly simplifies multithreading in our project.


1 Answers

RxJava is not "basically for async operation". That is only one aspect of it.

  1. RxJava allows you to compose operations on data, such that the output of one operation can be used as the input to the next. This operates similarly to streams implementations.
  2. RxJava uses this composability to allow some operations to occur in a multi-threaded environment. It does this by providing well-defined semantics for the operators when working with multiple schedulers. RxJava can use asyncTask to perform some operations.
  3. RxJava manages back pressure requirements for some applications by using Flowable chains, while Observable chains have no back pressure requirements. The former is appropriate where buffering is required or explicit acknowledgment of dropped information needs to be made.
  4. RxJava has clear error and error handling semantics.

asyncTask just handles asynchronous tasks.

like image 81
Bob Dalgleish Avatar answered Sep 20 '22 20:09

Bob Dalgleish