代码之家  ›  专栏  ›  技术社区  ›  Serg Burlaka

尝试创建group.toList(),但看起来有问题。但得到:只允许一个订户!如何修复代码?

  •  0
  • Serg Burlaka  · 技术社区  · 7 年前

    我的问题是加载不同类型的项,然后按类型排序

    type to items (for the type)  
    

    包装到对象:

    data class MyProWrap(val type: ProType, val profiles: ArrayList<ProImpl>)
    

    groupBy() 我试着通过 组.toList() 然后通过 阻止获取()

    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 
    }
    

    0 回复  |  直到 7 年前
        1
  •  0
  •   Serg Burlaka    7 年前

    前一天我已经找到了孤独 收集()) the post

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