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
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?