我遇到了一个小问题,我正在使用RxJava从服务器检索数据,然后用本地检查那些数据,问题我开始在本地检查文件并存储数据,然后我启动observable我得到在数据完全下载之前的SUCCESS然后我在成功后收到“initRemoteData”结果。
所以我需要做的是当observable给出结果我继续时调用“InitRemoteData”时阻塞observable。
private Observable<CheckStatus> getCheckObservable() {
return Observable.defer(() -> {
DataDTO dto;
try {
dto = Utils.decryptData(
localfile.getNonce(),
localfile.getEncryptedData(),
password);
} catch (WrongPasswordException e) {
return Observable.just(CheckStatus.WRONG_PASSWORD);
}
try {
storeDataPrefs(dto);
} catch (RuntimeException e) {
return Observable.just(CheckStatus.OTHER_ERROR);
}
storeDatabase(dto);
initRemoteData();//<-Here i did call for another observable but the observable keep going without waiting it to finish.
return Observable.just(CheckStatus.SUCCESS);
});
}
可观察的电话:
getCheckObservable()
.subscribeOn(BackgroundSchedulers.getMultiThreadInstance())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(Subscribers.create(checkResult -> replaceFragment(
CheckAccountCompleteFragment
.newInstance(checkResult),
AddToBackStack.FALSE)));
更新: initRemoteData代码:
Observable.zip(getObservable1()),//remote Data
getObservable2(), // local Data
(observableResult1,observableResult2)->{
doSomethingWith(observableResult1,observableResult2); // compare between data
return null;
}).subscribeOn(BackgroundSchedulers.getMultiThreadInstance())
.observeOn(AndroidSchedulers.mainThread())
.doOnCompleted(() -> {
doSomething2(); // update fields
}
})
.subscribe();
答案 0 :(得分:2)
如果我弄错了你需要让Observable.combineLatest()
等待两个结果?如果是:return
可以帮助您。
更新:
好的,那么为什么不只是Observable.zip(getObservable1()),//remote Data
getObservable2(), // local Data
(observableResult1,observableResult2)->{
doSomethingWith(observableResult1,observableResult2); // compare between data
return CheckStatus.SUCCESS; // <-- CHANGES
}).subscribeOn(BackgroundSchedulers.getMultiThreadInstance())
.observeOn(AndroidSchedulers.mainThread())
.doOnCompleted(() -> {
doSomething2(); // update fields
}
});
这个“等待”可观察的“大”观察者
像这样:
private Observable<CheckStatus> getCheckObservable() {
return Observable.defer(() -> {
DataDTO dto;
try {
dto = Utils.decryptData(
localfile.getNonce(),
localfile.getEncryptedData(),
password);
} catch (WrongPasswordException e) {
return Observable.just(CheckStatus.WRONG_PASSWORD);
}
try {
storeDataPrefs(dto);
} catch (RuntimeException e) {
return Observable.just(CheckStatus.OTHER_ERROR);
}
storeDatabase(dto);
return initRemoteData(); // <-- CHANGES
});
}
并且
def asterisk():
ast = "*"
i = 1
lines = int(input("How many asterisks do you want? "))
space = " "
for i in range(0, lines+1):
print (lines * space, ast*i)
lines -= 1
i += 1