Java rx嵌套异步调用 [英] Java rx nested asychronous calls

查看:194
本文介绍了Java rx嵌套异步调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用java rx库,在flatMap和map的内部调用中遇到问题,由于某些原因,大多数内部调用未完成.

I am working with the java rx library now, I am having problems with the internal calls with flatMap and map, for some reason the most internal call is not been done.

此外,如果我在内部调用中使用suscribe()替换flatMap(在childrenItemsResponse.forEach代码之前),则该代码将被执行,但执行不同步,这意味着此调用是在主flatMap之后完成的执行完成.

Besides, if I replace the flatMap with suscribe() in the internal call, (before the childrenItemsResponse.forEach code), this code is executed, bat the execution is not synchronous, I mean this call is done after the main flatMap execution finish.

这是我的代码:

override fun getSportList(dCSServiceContext: DCSServiceContext): Single<List<EntityBrowse>> {
        return dCSService.get(dCSServiceContext).flatMap { item ->
            val entityBrowseList = arrayListOf<EntityBrowse>()
            val section = builSection(item?.firstOrNull()!!)
            if (item.firstOrNull()?.collections?.get("NavItems")?.size!! > 0) {
                dCSServiceContext.contentIds = item.firstOrNull()?.collections?.get("NavItems")
                buildNavItems(dCSServiceContext).map { section ->
                    return@map section
                }.map { items ->
                    section.items = items
                    return@map entityBrowseList
                }
            } else {
                Single.just(entityBrowseList)
            }
        }
    } 

问题在buildNavItems方法中出现:

The problem is presented in the buildNavItems, method:

private fun buildNavItems(dCSServiceContext: DCSServiceContext): Single<MutableList<Item>> {
        return dCSService.get(dCSServiceContext).map { itemsResponse ->
            val items: MutableList<Item> = arrayListOf()
            itemsResponse.forEach { item ->
                val transformedItem = buildItem(item!!)
                if (item?.collections?.get("NavItems") != null) {
                    dCSServiceContext.contentIds = item?.collections?.get("NavItems")
                    val childrenItems: MutableList<Item> = arrayListOf()
                    dCSService.get(dCSServiceContext).flatMap { childrenItemsResponse ->
                        childrenItemsResponse.forEach { childrenItem ->
                            val transformedChildrenItem = buildItem(childrenItem!!)
                            childrenItems.add(transformedChildrenItem)
                        }
                        val section = Section("", "", false,childrenItems )
                        val data = Data(section)
                        val children = Children(data)
                        transformedItem.children = children
                        items.add(transformedItem)
                        Single.just(items)
                    }
                } else {
                    val transformedItem = buildItem(item!!)
                    items.add(transformedItem)
                }
            }
            return@map items
            //Single.just(items)
        }
    }

更具体地说,行代码中的dCSService.get(dCSServiceContext).flatMap { childrenItemsResponse ->

More specifically, the in the line code: dCSService.get(dCSServiceContext).flatMap { childrenItemsResponse ->

此代码永远不会执行.

我不确定是什么原因引起的.

I am not sure about what could be the cause of the issue.

提前谢谢!

推荐答案

要激活观察者链,必须进行订阅.您没有订阅以dCSService...开头的观察者链. flatMap()本身将不会订阅其内部可观测对象,除非已订阅了外部可观测链.

To activate an observer chain, there has to be a subscription. You don't subscribe to the observer chain that starts with dCSService.... flatMap() itself won't subscribe to its interior observables until the outer observable chain has been subscribed to.

这篇关于Java rx嵌套异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆