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")
Observable<UserWrapper> getUserSettings();
@GET("/{username}/collections")
Observable<List<Item>> getItems(@Path("username") String userName);
@GET("/item/{id}")
Observable<ItemInfo> getItemInfo(@Path("id") int id);
@GET("/{username}/friends")
Observable<List<Friend>> getFriends(@Path("username") String userName);
Here's what I want to do sequentially:
UserWrapper
by calling getUserSettings()
saveUser(userWrapper)
getItems(userWrapper.getUserName())
getItemInfo(item.getId())
itemInfo
by calling saveItem(itemInfo)
getFriends(userWrapper.getUserName())
saveFriend(friend)
Now I am new to RxJava and do not know how to handle lists. I watched one of Jake Wharton's slides and found that he uses a function flattenList()
but I don't know its definition. It would be great if you can help composing this chain.
Update 1
This is as far I've gotten now:
mApiService.getUserSettings()
.map(this::saveUser)
.flatMap(userWrapper -> mApiService.getItems(userWrapper.getUserName()));
.flatMapIterable( ? "How to iterate for each item" ? );
Update 2
I am trying to write something like this
mApiService.getUserSettings()
.map(this::saveUser)
.flatMap(userWrapper -> mApiService.getItems(userWrapper.getUserName()))
.someMethodToIterateThroughEachItem(item -> mApiService.getItemInfo(item))
.map(this::saveItem)
.someMethodThatCanCallUserWrapperAgain(userWrapper -> mApiService.getFriends(userWrapper.getUserName()))
.someMethodToIterateThoughEachFriend(friend -> saveFriend(friend))
Rx gives you a very granular control over which threads will be used to perform work in various points within a stream. To point the contrast here already, basic call approach used in Retrofit is only scheduling work on its worker threads and forwarding the result back into the calling thread.
RxJava can be used with any Java-compatible language, whereas Kotlin coroutines can only be written in Kotlin. This is not a concern for Trello Android, as we are all-in on Kotlin, but could be a concern for others. (Note that this just applies to writing code, not consuming it.
Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.
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(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With