我是RX的新手并且遇到了下一个:
我正在使用SQLBrite,并且在调用查询方法时,它会让我回到"无休止的"监听db更改的Observable。当一些项目被发出时 - 我需要在循环中进行一些异步调用并返回我收到的项目。示例代码:
public Observable<List<Entity>> execute() {
return someManager.getAccount()
.take(1) // I do not need RX to trigger updates when account changed, so use take(1)
.flatMap(accountOptional -> {
if (accountOptional.isPresent()) {
SomeAccount account = accountOptional.get();
return someManager.getEntityDataSource()
.query(new EntitySpecification(account.getId()));
}
return Observable.just(new ArrayList<Entity>()); // No account - no entities
})
.flatMapIterable(entities -> entities) // Here I have a list of entities i I need to make async calls to fill entities with some iiner data (some other entities)
.flatMap(this::loadInnerData)
.toList()
.toObservable();
}
private Observable<Entity> loadInnerData(Entity entity) {
return do some work with entity;
}
当我使用toList()时会遇到麻烦 - 它会等到Observable结束它的工作 - 但它不会被完成,因为这个observable正在监听db。我怎样才能听到不停地倾听“无休止”的声音。使用RX进行可观察和循环异步调用(就像使用loadInnerData()一样)?
答案 0 :(得分:0)
也许尝试用
return Observable.create(new Action1<Emitter<String>>() {
@Override
public void call(final Emitter<String> stringEmitter) {
// here u can use onNext / onError
stringEmitter.onNext(s);
stringEmitter.onError(e);
}
});
stringEmitter.setCancellation(new Cancellable() {
@Override
public void cancel() throws Exception {
subscription.unsubscribe();
}
});
}
}, Emitter.BackpressureMode.BUFFER);