I want to replace my AsyncTask with RxJava in android. My current AsyncTask goes like this:
public class ProgressBarAsyncTask extends AsyncTask<Void,Void,Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
ringProgressDialog = ProgressDialog.show(context,"MyProgressBarTitle","Working please wait",true, false);
}
@Override
protected Void doInBackground(Void... void) {
//do work
myTask();
return null;
}
@Override
protected void onPostExecute(Void void) {
super.onPostExecute();
ringProgressDialog.dismiss();
}
}
Here's my RxJava replacement:
public static Observable<Void> getObservable(final Context context,final String... params) {
return Observable.defer(new Func0<Observable<Void>>() {
@Override
public Observable<Void> call() {
return Observable.just(myTask());
}
});
}
public static Subscriber<Void> getSubscriber() {
Subscriber<Void> subscriber = new Subscriber<Void>() {
@Override
public void onCompleted() {
ringProgressDialog.dismiss();
}
@Override
public void onError(Throwable e) {
Log.d(TAG,e.toString());
}
@Override
public void onNext(Void aVoid) {
manipulateData();
}
};
return subscriber;
}
My Activity:
public class MainActivity extends Activity {
private ProgressDialog ringProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetNumberObservable.Observable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()))
.subscribe(getSubscriber());
}
}
How do I mimic the onPreExecute() method in the AsyncTask where I kick off the progressDialog?
Observable
lifecycle events listeners, in your case you want to do something
(update the UI) before the task started, which mean you want the
doOnSubscribe
event. (side note it is true with 'cold' Observables
that started thier work when subscrbied to - like your case) Just
beware to call .observeOn(AndroidSchedulers.mainThread()))
before
the doOnSubscribe
in order to get notified on the mainThread, as
you're updating the UI.Instead of using both defer
and just
return Observable.defer(new Func0<Observable<Void>>() {
@Override
public Observable<Void> call() {
return Observable.just(myTask());
}
});
you can use fromCallable
:
Observable.fromCallable(new Callable<Object>() {
@Override
public Object call() throws Exception {
return myTask();
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With