I want to use the progress bar with RxJava as you can with AsyncTask

I want to replace my AsyncTask with RxJava in android. My current AsyncTask is as follows:

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 is my replacement for RxJava:

  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; } 

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 imitate the onPreExecute () method in AsyncTask where I run progressDialog?

+5
source share
3 answers
  • In RxJava, you have do statements that create Observable listeners for life cycle events, in your case you want to do something (update the user interface) before running the task, which means that you want a doOnSubscribe event. (note that it’s true with a β€œcold” observation that their work started when they signed up for - as your case) Just beware of calling .observeOn(AndroidSchedulers.mainThread())) before doOnSubscribe to receive notification on mainThread, since you updating the user interface.
  • 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(); } }) 
+3
source

Here's how I do it:

 public final class ProgressOrResult<T> { final int progress; final T result; public ProgressOrResult(int progress, T result) { this.progress = progress; this.result = result; } } ProgressDialog ringProgressDialog = ProgressDialog.show( context, "MyProgressBarTitle", "Working please wait", true, false); Observable.fromEmitter((Emitter<ProgressOrResult> emitter) -> { // generate "progress" int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; emitter.onNext(new ProgressOrResult(i, null)); Thread.sleep(1); } // generate "result" emitter.onNext(new ProgressOrResult(100, sum)); emitter.onComplete(); }, BackpressureMode.BUFFER) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(result -> { if (pr.result == null) { ringProgressDialog.setProgress(pr.progress); } else { ringProgressDialog.dismiss(); // process the result here } }, error -> { // handle error here }) 
+1
source

Shameless advertisement

I created the RxLoading library for this, it can do it and much more,

you can just do something like this:

 networkCall().compose(RxLoading.<>create(loadingLayout)).subscribe(...); 

it consists of 2 classes, a custom view (loadLayout) and RxLoading, which is a GitHub page .

0
source

Source: https://habr.com/ru/post/1264403/


All Articles