简体   繁体   English

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

[英]Try to make group.toList() but looks questionably. But got: Only one subscriber allowed! How to fix the code?

My problem is to load items with different types , then to sort it by types 我的问题是加载不同类型的项目,然后按类型对其进行排序

and return pair results: 和返回结果:

type to items (for the type)  

wrapping to object: 包装到对象:

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

so after groupBy() i trying get items list via group.toList() and then extract via blockingGet() 所以在groupBy()后我尝试通过group.toList()获取项目列表,然后通过blockingGet()提取

my code: 我的代码:

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

but got error: 但得到了错误:

java.lang.IllegalStateException: Only one subscriber allowed!

my wrap class is: 我的包装类是:

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

  enum class ProType {
     FIRST_TYPE,
     SECOND_TYPE,
     THIRD_TYPE 
}

there is the way to fix it? 有解决方法吗?

For some daytime ago i have already found solition via collect() operator thru the post 对于某些白天,我已经通过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.

相关问题 仅一个连接允许接收订户 - Only one connection receive subscriber allowed RxJava:“java.lang.IllegalStateException:只允许一个订阅者!” - RxJava: “java.lang.IllegalStateException: Only one subscriber allowed!” Webflux post 请求:只允许一个连接接收订阅者 - Webflux post request: Only one connection receive subscriber allowed 当响应具有HTTP错误状态代码时,为什么会出现“只允许一个连接接收订户”? - Why do I get `Only one connection receive subscriber allowed` when the response has an HTTP error status code? 如何解决“仅允许一种身份验证机制; 只有X-Amz-Algorithm查询参数…” - How to fix “Only one auth mechanism allowed; only the X-Amz-Algorithm query parameter…” 当响应不包含媒体类型头时,Spring WebClient抛出“仅允许一个连接接收订阅者” - Spring webclient throws “Only one connection receive subscriber allowed” when response does not contain media type header 响应正文为空时,“IllegalStateException:仅允许一个连接接收订阅者” - "IllegalStateException: Only one connection receive subscriber allowed" when response body is empty 如何让一个订阅者在 Radisson(redis-java 客户端)中处理消息(考虑有多个) - How to make one subscriber to process message (consider having more than one) in Radisson (redis-java client) Java Collectors.toList()仅创建一个元素 - Java, Collectors.toList() creates only one element 如何从 stream 中仅提取一个允许的元素? - How to extract only one allowed element from a stream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM