在RxJava2中向CompositDisposable添加Maybe

时间:2017-07-14 03:48:40

标签: android rx-java2

在我的活动中,我有以下内容,导致错误消息'错误:(190,35)错误:不兼容的类型:MaybeObserver无法转换为Disposable' 。我希望这可行,因为我已经在使用Completable和DisposableCompletableObserver组合做了类似的事情。如何在RxJava2中使用带有CompositeDisposable的Maybe?

private final CompositeDisposable disposables = new CompositeDisposable();

// ...

String id = authManager.getUserID();

Maybe maybe = userManager.getUser(id);

disposables.add(maybe
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeWith(new DisposableMaybeObserver() {
                           @Override
                           public void onSuccess(Object o) {

                           }

                           @Override
                           public void onError(Throwable e) {

                           }

                           @Override
                           public void onComplete() {

                           }
                       }));

1 个答案:

答案 0 :(得分:1)

您应该使用ResourceMaybeObserver

private final CompositeDisposable disposables = new CompositeDisposable();

String id = authManager.getUserID();

Maybe<Object> maybe = userManager.getUser(id);


disposables.add(maybe
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new ResourceMaybeObserver<Object>() {
                @Override
                public void onSuccess(@NonNull Object o) {

                }

                @Override
                public void onError(@NonNull Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            }));