尽管实现了doOnError,但RxAndroid导致应用崩溃
作者:互联网
我有以下代码.基本上,这是尝试将所有数据从特定SQLite表发送到DynamoDB:
Observable.create(new Observable.OnSubscribe<Area>() {
@Override
public void call(Subscriber<? super Area> subscriber) {
try {
for (Area item : areaDao.listAll()) {
subscriber.onNext(item);
}
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
}
}).flatMap(new Func1<Area, Observable<Area>>() {
@Override
public Observable<Area> call(Area area) {
dynamoDBMapper.save(area);
return Observable.just(area);
}
}).observeOn(
AndroidSchedulers.mainThread()
).doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.w("AreaHandler", "Could not upload area", throwable);
}
}).doOnCompleted(new Action0() {
@Override
public void call() {
Toast.makeText(ctx, R.string.toast_upload_successful, Toast.LENGTH_SHORT).show();
}
}).subscribeOn(
Schedulers.io()
).subscribe(new Action1<Area>() {
@Override
public void call(Area area) {
areaDao.delete(area.getId());
}
});
我正在尝试在禁用Internet连接的仿真器上运行它,然后发生的情况是Dynamo客户端执行了两次(失败的)重试,然后引发异常并使应用程序崩溃.根据我在文档中所读的内容,该异常应该被doOnError吞噬,而不是被放任不管而杀死进程.
我想念什么?
解决方法:
您在错误的位置抓住错误. doOnError用于副作用.它不处理错误.
选项1.传入两个Action1
Observable.just(1, 2, 3)
.subscribe(
new Action1<Integer>() {
@Override
public void call(Integer integer) {
System.out.println(integer);
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
System.err.print(throwable);
}
});
选项2:传递观察者
Observable.just(1, 2, 3)
.subscribe(new Observer<Integer>() {
@Override
public void onCompleted() {
}
@Override
public void one rror(Throwable throwable) {
System.err.print(throwable);
}
@Override
public void onNext(Integer integer) {
System.out.println(integer);
}
});
标签:rx-java,rx-android,android 来源: https://codeday.me/bug/20191027/1941528.html