尝试使group.toList()看起来很可疑。但是得到了:只允许一个订户!如何修复代码?

时间:2019-03-20 16:24:35

标签: java kotlin rx-java2

我的问题是加载不同类型的项目,然后按类型对其进行排序

并返回配对结果:

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 
}

有解决方法吗?

1 个答案:

答案 0 :(得分:0)

一天前,我已经通过the post

通过 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}") 
                          })