java - 尝试制作 group.toList() 但看起来有问题。但是得到了:只允许一个订阅者!如何修复代码?
问题描述
我的问题是加载不同类型的项目,然后按类型对其进行排序
并返回对结果:
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()操作员通过帖子找到了解决方案
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}")
})
推荐阅读
- flutter - Flutter:在小部件调用中使用参数(图标)
- angular - ngfor 用于具有 ng2-charts 的对象中的数组
- python - 如何仅保留数据框列的字符串中的数字?
- python - Python:每次运行函数时如何增加代码计数
- ios - 如何使多个协议符合一个类并设计方法来输入和打印详细信息?
- java - 如何在 GraphQL 中使用参数列表创建查询?
- javascript - 如何让机器人回复表情符号?
- html - 按索引删除不起作用。角
- if-statement - If语句具有多个agruments来捕获鼠标位置
- mysql - sql-query 获取包含事务计数的报告