Skip to main content
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link

Async Task using RxJava2 in Android

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().