[英]Try to make group.toList() but looks questionably. But got: Only one subscriber allowed! How to fix the code?
我的問題是加載不同類型的項目,然后按類型對其進行排序
和返回結果:
type to items (for the type)
包裝到對象:
data class MyProWrap(val type: ProType, val profiles: ArrayList<ProImpl>)
所以在groupBy()后我嘗試通過group.toList()獲取項目列表,然后通過blockingGet()提取
我的代碼:
proRepo.loadUser(prefSource.userId)
.flatMap {
Flowable.fromIterable(it)
}
.groupBy { it.typePro}
.flatMap { group ->
group.map {
group.key to group.toList() //<- try collect result to list
}
}
.map {
MyProWrap(
type = it.first!!,
profiles = ArrayList(it.second.blockingGet()) //<- try extract result from Single via blocking get
)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
result = it
}
}, {
Timber.e("Fail to load profiles. ${it.message}")
})
但得到了錯誤:
java.lang.IllegalStateException: Only one subscriber allowed!
我的包裝類是:
data class MyProWrap(val type: ProType, val profiles: ArrayList<ProItem>)
enum class ProType {
FIRST_TYPE,
SECOND_TYPE,
THIRD_TYPE
}
有解決方法嗎?
對於某些白天,我已經通過collect()操作員通過帖子找到了solition
proRepo.loadUser(prefSource.userId)
.flatMap { Flowable.fromIterable(it) }
.groupBy { it.typePro } // <- group by profile type
.flatMap { group ->
group.collect({ ArrayList<ProItem>() }, { profiles, profile ->
profiles.add(profile) //<- get profiles of one type
}).map {
MyProWrap(type = group.key!!, profiles = it) //<- wrap result
}.toFlowable()
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
result = it
},{
Timber.e("Fail to load profiles. ${it.message}")
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.