问题描述
我正在create()
的帮助下手动创建Observable
.现在在里面,我检查一些条件 &基于此,我想通知订阅者有关错误.这是我创建可观察对象的方式:
I am creating Observable
manually with the help of create()
. now inside, I check some conditions & based on that, I would like to notify the subscriber about error. Here's how I am creating observable:
public Observable<User> loginUser(String email, String password) {
return Observable.create(
emitter -> {
myAsynchronousWork.onCompleteListener(
result -> {
if(!result.isSuccess()) {
// This causes the crash.
emitter.onError(new Throwable(result.getError()));
} else {
// Process result & create User object & return it. This works as expected.
emitter.onNext(user);
emitter.onComplete();
}
}
);
}
);
}
&然后我订阅 loginUser()
就像:
& then I subscribe to loginUser()
like:
loginUser("", "")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(user -> {
Log.d("TAG", "logged in user => " + user.getUuid());
Log.d("TAG", "user name => " + user.getUserName());
Log.d("TAG", "user email => " + user.getEmailId());
}, throwable -> {
Log.e("TAG", "error in login => " + throwable.getMessage());
}, () -> {
});
我希望调用 emitter.onError()
应该在 loginUser()
的 subscribe()
的 onError 里面,我已经记录了异常,但应用程序因 emitter.onError()
在 logcat 中返回的异常而崩溃,就像没有人来处理它一样!
I expect that calling emitter.onError()
should go inside onError of subscribe()
of loginUser()
where I have Logged the exception, but instead the app gets crashes with the Exception returned by emitter.onError()
in logcat like there's no one to handle it!
我通过调试检查过 &发现当它在线 emitter.onError()
时,emitter
是 "null"
.然而 onNext &onComplete 不会造成任何问题.请让我知道我哪里做错了?
I checked by debugging & found that while it's on line emitter.onError()
, emitter
was "null"
. however onNext & onComplete doesn't cause any problem.Please let me know where I am doing wrong?
推荐答案
我的应用程序崩溃的原因是,我实际上有这样的事情:
The reason why my app was getting crashed was, I had actually something like this:
myAsynchronousWork.onCompleteListener(
result -> {
if(!result.isSuccess()) {
// This causes the crash.
emitter.onError(new Throwable(result.getError()));
} else {
// Process result & create User object & return it. This works as expected.
emitter.onNext(user);
emitter.onComplete();
}
},
exception -> {
emitter.onError(exception); // This was the reason of problem!
}
);
我没有发布完整的部分,因为我没有意识到它导致了异常.
I didn't posted the full part because I didn't get that it was causing the exception.
这里发生的事情是,我的代码首先进入了 exception->
部分,它通知观察者关于错误 &那个观察员被终止了.现在我的 result->
部分正在执行 &在这里,当我再次尝试调用 emitter.onError()
时,它崩溃了,因为没有 emitter
来处理这个 &RxJava2 正在全局抛出它.
What happening here was, my code first was going inside the exception->
part which was notifying the observer about the error & that observer was getting terminated. now my result->
part was getting executed & here when I tried to call again emitter.onError()
, it was crashing as there was no emitter
to handle this & RxJava2 was throwing it globally.
所以对我来说,我删除了 exception ->
部分,因为它可以在 result ->
上检查结果 &我还用
So for me, I removed the exception ->
part as it was any how going to be on result ->
where I could check the result & I have also wrapped the emitter.onError with
if(!emitter.isDisposed()) emitter.onError();
因为在我的情况下,如果处理了发射器,可以忽略错误.
because in my case, it was fine to ignore the error if emitter was disposed.
这篇关于Android RxJava2 应用程序在从创建的 observable 中调用emitter.onError() 时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!