我是这个图书馆的新手,所以如果你有任何提示或建议,我们非常欢迎。 我让RxJava2运行并执行了一个longNetworkOperation假方法,流程如下:
实现将执行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
}})
在主线程中获取结果:.observeOn(AndroidSchedulers.mainThread())
添加获得结果通知的观察者:.subscribe(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.
}
};
这样可以正常工作,没有设备旋转,但是如果在doLongNetworkOperation在后台运行时旋转设备,则TextView tvRxJava不会更新值。 甚至没有新的注射。
tvRxJava = (TextView) findViewById(R.id.tv_rxjava);
如何使用RxJava2管理android中的方向更改?
这是我的gradle dep:
compile 'io.reactivex.rxjava2:rxjava:2.0.5'//rxjava2
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' //rxjava2 for android