-1

I'm using RxJava3 Observers and Observables. But i have a problem.

I have attached a screenshot below, please see the Toast on the bottom. I used Thread.currentThread().toString() And i got the Main, 5, Main output

Screenshot1

Now, here's my observable code

private void start(){
        reactiveDataSource.getObservableList().subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e(TAG, "subscribed in observable list");
                
            }
            
            @Override
            public void onNext(String item) {
                Log.e(TAG, "in observable list ->" + item);
                if(item == "D"){
                    Toast.makeText(getApplicationContext(), Thread.currentThread().toString(), Toast.LENGTH_SHORT).show();
                } else {
                    if(item == "E"){
                        Toast.makeText(getApplicationContext(), "E", Toast.LENGTH_SHORT).show();
                    } else {
                        if(item == "F"){
                            Toast.makeText(getApplicationContext(), "F", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                
            }
            
            @Override
            public void onError(Throwable e) {
                Log.e(TAG, "error in observable list " + e.getMessage());
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
                
            }
            
            @Override
            public void onComplete() {
                Log.e(TAG, "in observable list complete");
                Toast.makeText(getApplicationContext(), "Complete!!!", Toast.LENGTH_SHORT).show();
                
            }
        });
    }

ReactiveDataSource.java:

public Observable<String> getObservableList() {
        List<String> createdList = new ArrayList<>();
        createdList.add("D");
        createdList.add("E");
        createdList.add("F");
        createdList.add("G");
        return Observable.fromIterable(createdList);
    }

I want to run my task inside onNext() method in the background thread. How do i achieve this?

My Main UI thread still freezes during intensive tasks and Thread.sleep(5000); makes my MainUI freeze even if i placed this code on filter() operator of rxjava, trying to test and freeze my new thread.

Is there something I'm doing wrong?

1 Answers1

0

observeOn puts your onNext back to the main thread, remove it and your subscribe will run on the io thread.

I assume you still want to do something on the main thread afterwards so insert a doOnNext with your computation before using observeOn:

reactiveDataSource.getObservableList().subscribeOn(Schedulers.io())
        .doOnNext(item -> {
             // your computation here
        })
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<String>() {
             // observe the outcome on the main thread
        });
akarnokd
  • 69,132
  • 14
  • 157
  • 192