I'm new at this library, so if you got any tips or suggestions are more than welcome. I got RxJava2 running and executing a longNetworkOperation fake method, the flow is this:
- Create the observable: Observable.just("str1")
- Change the execution thread: .subscribeOn(Schedulers.newThread())
Implement the map interface which will execute the LongRunningProcess:
.map(new Function<String, String>() { @Override public String apply(String cad){ // cad is the input value Log.d(TAG,"long operation in thread: "+Thread.currentThread().getName()); //output thread name return " longNetworkOperation result => "+doLongNetworkOperation(); //here we do the background operations }})Get the results in Main Thread: .observeOn(AndroidSchedulers.mainThread())
Add the observer which get notified for the results: .subscribe(observer);
And the observer:
Observer observer = new Observer() {
@Override
public void onSubscribe(Disposable d) { Log.d(TAG,"onSubscribe, thread name >> "+Thread.currentThread().getName()); }
@Override
public void onNext(Object value) { Log.d(TAG,"on next, input value:<<"+value.toString()+">> thread name >>"+Thread.currentThread().getName());}
@Override
public void onError(Throwable e) { Log.d(TAG,"error >>"+e.toString()); }
@Override
public void onComplete() {
Log.d(TAG,"onCompleted thread name >> "+Thread.currentThread().getName());
tvRxJava = (TextView) findViewById(R.id.tv_rxjava); //update the reference of tvRxJava.
tvRxJava.setText("process executed..."); //this line does not change the value of the TextView tvRxJava.
}
};
This works fine, with no device rotation, but if you rotate the device while doLongNetworkOperation is running in the background the TextView tvRxJava does not update value. not even with a new inject.
tvRxJava = (TextView) findViewById(R.id.tv_rxjava);
How do you manage orientation change in android with RxJava2??
This is my gradle dep:
compile 'io.reactivex.rxjava2:rxjava:2.0.5'//rxjava2
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' //rxjava2 for android