Fshamri Fshamri - 2 months ago 31
Android Question

Convert AsyncTask to RxAndroid

I have the following method to post response to UI using otto and

AsyncTask
.

private static void onGetLatestStoryCollectionSuccess(final StoryCollection storyCollection, final Bus bus) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
bus.post(new LatestStoryCollectionResponse(storyCollection));
return null;
}
}.execute();
}


I need help to convert this
AsyncTask
to
RxJava
using RxAndroid library.

Answer

Don't use .create() but use .defer()

Observable<File> observable = Observable.defer(new Func0<Observable<File>>() {
  @Override public Observable<File> call() {

    File file = downloadFile();

    return Observable.just(file);
  }
});

to know more details see https://speakerdeck.com/dlew/common-rxjava-mistakes