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();
}
}
);
}
);
}
& 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());
}, () -> {
});
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!
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?