12

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:

  • Get UserWrapper by calling getUserSettings()
  • Save the user by calling saveUser(userWrapper)
  • Get user's items by calling getItems(userWrapper.getUserName())
  • Get each item's information by calling getItemInfo(item.getId())
  • Save each itemInfo by calling saveItem(itemInfo)
  • Get user's friends by calling getFriends(userWrapper.getUserName())
  • Save each friend by calling 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))
2
  • 1
    What is someMethodToIterateThroughEachItem? How does that compile? Commented Oct 5, 2015 at 15:35
  • Please give more details about someMethodToIterateThroughEachItem, someMethodThatCanCallUserWrapperAgain, and someMethodToIterateThoughEachFriend Commented Dec 9, 2015 at 6:22

1 Answer 1

20

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(...);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @zsxwing, I have gotten to this point already. Please check my updated question How do I iterate through each item in this call. Isn't there any built in function in RxJava already?
Could you elaborate what you want to do?
I've updated the question again for your better understanding. Please read bullet points too. Hope this answers your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.