RxJava icon indicating copy to clipboard operation
RxJava copied to clipboard

How to chain calls, depending on result, the second call is a recursive method?

Open rasilvap opened this issue 7 years ago • 1 comments

Hi, I am working with java rx version 1.2.10

In my Api, I have to chain two async calls : The first call gives me items The second call gives me the subItems from these item, but if the subItems have more "subItems" I have to call recursively the second method and add the respective subItems.

In my code, I wrote something like for the firts call :

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")
               //This is the second call.
               buildNavItems(dCSServiceContext).map { sectionItems ->
                   return@map sectionItems
               }.map { items ->
                   section.items = items
                   return@map entityBrowseList
               }
           } else {
               Single.just(entityBrowseList)
           }
       }
   }

In the recursive buildNavItems, I have the next:

 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")
                   
                    //Alternative 2: Recursion to fill the internal items, this code is not been called now.
                    buildNavItems(dCSServiceContext).map { childrenItems ->
                        val section = Section("", "", false,childrenItems )
                        val data = Data(section)
                        val children = Children(data)
                        transformedItem.children = children
                        items.add(transformedItem)
                    }

                } else {
                    val transformedItem = buildItem(item!!)
                    items.add(transformedItem)
                }
            }
            return@map items
            //Single.just(items)
        }
    }

The issue is that the buildNavItems(dCSServiceContext).map recursive call is never been done, I had tried using flatMap also but, after the end of the execution, this code is never been called, and those subItems are not been added to the final list.

rasilvap avatar Dec 24 '18 16:12 rasilvap

Hi. You could look into flatMap and a serialized UnicastSubject to allow a feedback loop to continue while more recursive items need to be extracted. You can also look into expand

Note though that RxJava 1 is no longer supported and generally, such questions are better suited for StackOverflow where there could be already a similar question and answer.

akarnokd avatar Dec 24 '18 18:12 akarnokd