RXJava.确定 for 循环中的所有 obervable 何时完成 [英] RXJava. Identify when all obervables inside for loop are finished
问题描述
我想执行以下操作.我有一个交易列表,我想通过为每个交易发出 2 个 api 请求(我正在使用 Retrofit2)来更新这些交易,然后将结果保存到数据库中(使用观察者).经过一番搜索,我决定使用 zip 运算符来组合 2 个请求,但我遇到的问题是我无法确定整个过程何时完成以更新 UI.代码看起来像这样.
I want to perform the following. I have a list of transactions which i want to update by making 2 api requests (i am using retrofit2), for each transactions and then saving the result into a database(using the observer). After some searching i decided to use zip operator to combine the 2 requests but the issue that i'm have is that i cannot identify when the whole process is finished to update the UI. Code looks like this.
for (Transaction realmTransaction : allTransactions) {
Observable<Map<String, String>> obs1 = getObs1(realmTransaction);
Observable<Map<String, String>> obs2= getObs2(realmTransaction);
Observable.zip(obs1, obs2,
(map1, map2) -> {
Map<String, String> combined = new HashMap<>();
// do some processing and return a single map after
return combined;
})
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(realmTransaction));
}
public Observer<Map<String, String>> getObserver(Transaction t){
return new Observer<Map<String, String>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Map<String, String> stringStringMap) {
// update database
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
}
}
我拥有的观察者更新了 realmTransaction 对象的一个字段.
The observer that i have updates a field of the realmTransaction object.
我的问题是如何在 for 循环结束时收到通知?
我想在整个过程完成后发送一个事件(可能使用 EventBust)以启动其他方法.
I would like to sent an event (maybe use EventBust) after the whole process has finish to kick off some other method.
谢谢
我还有一个小问题是关于我在 zip 运算符中提供的函数,我如何指定该函数将在哪个线程上运行?我想为此使用计算线程,这就是为什么我将observeOn 两次,但我在任何地方都找不到答案
Also another small question that i have is about the function that i provide inside the zip operator, how can i specify on which thread that function will run on? I would like to use a computation thread for that thats why i put observeOn twice, but I couldnt find an answer anywhere
推荐答案
每当你有 for 循环时,你应该考虑 range
、fromArray
或 fromIterable代码>.此外,您可能不需要完整的
subscribe
而是 doOnNext()
:
Whenever you have a for loop, you should think about range
, fromArray
or fromIterable
. In addition, you may not need the full subscribe
but doOnNext()
:
Observable.fromIterable(allTransactions)
.flatMap(realmTransaction -> {
Observable<Map<String, String>> obs1 = getObs1(realmTransaction);
Observable<Map<String, String>> obs2= getObs2(realmTransaction);
return Observable.zip(obs1, obs2, (map1, map2) -> {
Map<String, String> combined = new HashMap<>();
// do some processing and return a single map after
return combined;
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(stringStringMap -> handle(stringStringMap, realmTransaction));
})
.ignoreElements()
.subscribe(() -> handleCompleted(), e -> handleError(e));
这篇关于RXJava.确定 for 循环中的所有 obervable 何时完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!