RxJava: How to convert List of objects to List of another objects

后端 未结 9 474
悲哀的现实
悲哀的现实 2021-01-31 07:30

I have the List of SourceObjects and I need to convert it to the List of ResultObjects.

I can fetch one object to another using method of ResultObject:

c         


        
9条回答
  •  礼貌的吻别
    2021-01-31 07:51

    As an extension to Noel great answer. Let's say transformation also depends on some server data that might change during subscription. In that case use flatMap + scan.

    As a result when id list changes, transformations will restart anew. And when server data changes related to a specific id, single item will be retransformed as well.

    fun getFarmsWithGroves(): Observable> {
    
            return subscribeForIds() //may change during subscription
                    .switchMap { idList: Set ->
    
                        Observable.fromIterable(idList)
                                .flatMap { id: String -> transformId(id) } //may change during subscription
                                .scan(emptyList()) { collector: List, candidate: FarmWithGroves ->
                                    updateList(collector, candidate)
                                }
                    }
        }
    

提交回复
热议问题