我正在使用RXJava 2来执行操作。我希望每5秒钟进行一次操作。但是,在引入interval
方法后,它会更改我的功能并破坏我的.subscribe(DisponsableObserver<Boolean>)
mObserver = getObserver(); //return DisponsableObserver<Boolean>
observable = getSolultionObservable(); //return Observer<Boolean>
observable.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.interval(5, TimeUnit.SECONDS)
.subscribe(mSolutionObserver); //Now Produces Cannot Resolve Method error
如何在计时器上成功运行我的observable并返回布尔值?
答案 0 :(得分:1)
您可以使用flatMap
运算符将long
每5秒发出的项目interval
转换为Observable
Observable.interval(5, TimeUnit.SECONDS)
.flatMap(long -> observable)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mSolutionObserver);
修改强>: 没有lambda表达式:
Observable.interval(5, TimeUnit.SECONDS)
.flatMap(new Function<Long, ObservableSource<Boolean>>() {
@Override
public ObservableSource<Boolean> apply(Long aLong) throws Exception {
return observable;
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mSolutionObserver);