0
\$\begingroup\$

I have tried to create the async task using the RxJAVA2. The code is working fine. WHat enhancement can I do in this code?

private void createObservable(){
 progressDialog =new ProgressDialog(ProductActivityRx.this);
 progressDialog.setTitle("Data fetching...");
 progressDialog.setCanceledOnTouchOutside(false);
 progressDialog.show();
 Observable<List<Product>> booksObservable =Observable.fromCallable(new Callable<List<Product>>() {
 @Override
 public List<Product> call() throws Exception {
 return getProductList();
 }
 }).doOnNext(new Consumer<List<Product>>() {
 @Override
 public void accept(List<Product> strings) throws Exception {
 }
 });
 productSubscription = booksObservable.
 subscribeOn(Schedulers.io()).
 observeOn(AndroidSchedulers.mainThread()).
 subscribe(new Consumer<List<Product>>() {
 @Override
 public void accept(List<Product> productList) throws Exception {
 displayData(productList);
 }
 });
 }

I am updating the UI in displayData().

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jun 12, 2018 at 14:37
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
  1. in general, I believe it would be better if you included this in some sort of chain. What I mean here:

    • don't return void, rather an Observable
    • don't show the progress dialog in the function, rather delegate it to the part responsible for UI. Same with displayData()
    • where's the whole function called? What calls it? Where is it located, in an Activity?
  2. doOnNext() can be gotten rid of - there's no action in here

  3. Are you getting just one result at a time? I mean, is the function called perpetually on and on and on, and you just evaluate the result each time? Perhaps you could use Single or Maybe, which is better suited for single results.

Good luck and hope we'll both learn something along the way.

answered Aug 13, 2018 at 8:46
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.