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

后端 未结 9 476
悲哀的现实
悲哀的现实 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:47

    Using toList() waits for the source observable to complete, so if you do that on an infinite observable (like one bound to UI events and Database calls), you will never get anything in your subscribe().

    The solution to that is to Use FlatMapSingle or SwitchMapSingle.

    Observable> observable =   noteDao.getAllNotes().flatMapSingle(list -> Observable.fromIterable(list).toList());
    

    Now,everytime your list is updated, you can make that event behave as an observable.

提交回复
热议问题