Handling lists with RxJava and Retrofit in android

后端 未结 1 1081
借酒劲吻你
借酒劲吻你 2021-01-31 21:55

I have couple of api calls to make (sequentially, asynchronously) and some of them returns lists. My api interface is given below.

@GET(\"/users/settings\")
Obse         


        
相关标签:
1条回答
  • 2021-01-31 22:39

    RxJava has added flatMapIterable. So you don't need flattenList now. E.g.,

      Observable<UserWrapper> o =
           getUserSettings()
           .doOnNext(this::saveUser)
           .flatMap(user -> getItems(user.getUserName())
                            .flatMapIterable(items -> items)
                            .flatMap(item -> getItemInfo(item.getId()))
                            .doOnNext(this::saveItem)
                            .toList()
                            .map(ignored -> user))
            .flatMap(user -> getFriends(user.getUserName())
                             .flatMapIterable(friends -> friends)
                             .doOnNext(this::saveFriend)
                             .toList()
                             .map(ignored -> user)
            );
        o.subscribe(...);
    
    0 讨论(0)
提交回复
热议问题