[android] rxJava1→rxJava2 변경~unsubscribe에서 dispose로~

11957 단어 AndroidRxJava2RxJava

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
○ 떨어진 것 모두 클릭




좋은 웹페이지 즐겨찾기