如何遍历列表,以及何时完成使用 RXJava 启动方法
How to iterate over a list, and when finish launch a method with RXJava
我有一个数据模型列表,所以我必须应用一种方法 returns 一个视图。
当一切都计算完后,我必须启动一个方法,它会进行另一种计算。
问题是,正如我所拥有的,在第二种方法的每次迭代中都会启动。(当然我遗漏了一些东西或做错了,但我对 RX 的了解很低)
是否可以对每个方法进行所有计算,并在完成后仅启动该方法一次?
val markersViewList = hashMapOf<String, View>()
val subscription = Observable.fromIterable(retrivedUserInfoList)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.map { userInfo ->
val markerLayout = setupUpForMarkerLayout(userInfo)
if (markerLayout != null) {
if (userInfo.userId == owner.uid) { //is owner
markerViewList[OWNER] = markerLayout
} else {
if (!markerViewList.containsKey(userInfo.data1)) {
markerViewList[userInfo.data1] = markerLayout
}
}
}
}
.subscribe {
//THIS IS THE METHOD THAT ONLY HAS TO BE CALCULATED ONCE
createImages(retrivedUserInfoList,markerViewList)
}
addSubscription(subscription)
您可以使用 ignoreElements() 运算符:
val markersViewList = hashMapOf<String, View>()
val subscription = Observable.fromIterable(retrivedUserInfoList)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.map { userInfo ->
val markerLayout = setupUpForMarkerLayout(userInfo)
if (markerLayout != null) {
if (userInfo.userId == owner.uid) { //is owner
markerViewList[OWNER] = markerLayout
} else {
if (!markerViewList.containsKey(userInfo.data1)) {
markerViewList[userInfo.data1] = markerLayout
}
}
}
}
.ignoreElements()
.subscribe {
//THIS IS THE METHOD THAT ONLY HAS TO BE CALCULATED ONCE
createImages(retrivedUserInfoList, markerViewList)
}
addSubscription(subscription)
它将把你的 Observable
变成 Completable
所以你的 subscribe
块在完成时只会被调用一次。
我有一个数据模型列表,所以我必须应用一种方法 returns 一个视图。
当一切都计算完后,我必须启动一个方法,它会进行另一种计算。
问题是,正如我所拥有的,在第二种方法的每次迭代中都会启动。(当然我遗漏了一些东西或做错了,但我对 RX 的了解很低)
是否可以对每个方法进行所有计算,并在完成后仅启动该方法一次?
val markersViewList = hashMapOf<String, View>()
val subscription = Observable.fromIterable(retrivedUserInfoList)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.map { userInfo ->
val markerLayout = setupUpForMarkerLayout(userInfo)
if (markerLayout != null) {
if (userInfo.userId == owner.uid) { //is owner
markerViewList[OWNER] = markerLayout
} else {
if (!markerViewList.containsKey(userInfo.data1)) {
markerViewList[userInfo.data1] = markerLayout
}
}
}
}
.subscribe {
//THIS IS THE METHOD THAT ONLY HAS TO BE CALCULATED ONCE
createImages(retrivedUserInfoList,markerViewList)
}
addSubscription(subscription)
您可以使用 ignoreElements() 运算符:
val markersViewList = hashMapOf<String, View>()
val subscription = Observable.fromIterable(retrivedUserInfoList)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.map { userInfo ->
val markerLayout = setupUpForMarkerLayout(userInfo)
if (markerLayout != null) {
if (userInfo.userId == owner.uid) { //is owner
markerViewList[OWNER] = markerLayout
} else {
if (!markerViewList.containsKey(userInfo.data1)) {
markerViewList[userInfo.data1] = markerLayout
}
}
}
}
.ignoreElements()
.subscribe {
//THIS IS THE METHOD THAT ONLY HAS TO BE CALCULATED ONCE
createImages(retrivedUserInfoList, markerViewList)
}
addSubscription(subscription)
它将把你的 Observable
变成 Completable
所以你的 subscribe
块在完成时只会被调用一次。