[android] rxJava1→rxJava2 변경~unsubscribe에서 dispose로~
rxJava1의 경우
RxJavaActivity.java
public class RxJavaActivity extends Activity {
private Subscription subscription;
public void onDestroy() {
if(subscription != null){
subscription.unsubscribe();
}
super.onDestroy();
}
private void loadData() {
subscription = Observable.create(new Observable.OnSubscribe<Data>() {
@Override
public void call(Subscriber<Data> subscriber) {
Data data = null;
try {
data = getData();
} catch (Exception e) {
subscriber.onError(e);
}
observableEmitter.onNext(data);
observableEmitter.onCompleted();
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscribe<Data>() {
@Override
public void onCompleted() {
//処理
}
@Override
public void onError(Throwable e) {
//処理
}
@Override
public void onNext(PixivResponse pixivResponse) {
//処理
}
});
}
}
rxJava2의 경우
RxJava2Activity.java
public class RxJava2Activity extends Activity {
private DisposableSingleObserver<Document> loadDataDisposable;
public void onDestroy() {
if(loadDataDisposable != null){
loadDataDisposable.dispose();
}
super.onDestroy();
}
private void loadData() {
loadDataDisposable = Observable.create(new ObservableOnSubscribe<Data>() {
@Override
public void subscribe(ObservableEmitter<Object> observableEmitter) throws Exception {
Data data = null;
try {
data = getData();
} catch (Exception e) {
if (!observableEmitter.isDisposed()) {
observableEmitter.onError(e);
}
}
if (!observableEmitter.isDisposed()) {
observableEmitter.onNext(data);
observableEmitter.onComplete();
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<Data>() { //subscribeWithになる
@Override
public void onNext(Document document) {
//処理
}
@Override
public void onError(Throwable throwable) {
//処理
}
@Override
public void onComplete() { //onCompleteになる
//処理
}
});
}
}
다음은 게임 홍보m()m○ 떨어진 것 모두 클릭
Reference
이 문제에 관하여([android] rxJava1→rxJava2 변경~unsubscribe에서 dispose로~), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/haruki99/items/8adb4f17471a75237940텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)