Processing lists using rxjava and retrofit

I have several API calls (sequential, asynchronous), some of which return lists My API interface is as follows

@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 is the order I want to do:

>Get the userwrapper by calling getusersettings() > save the user by calling saveuser (userwrapper) > get the user's items by calling getitems (userwrapper. Getusername()) > get the information of each item by calling GetItemInfo (item. Getid()) > save each iteminfo by calling saveitem (iteminfo) > save each iteminfo by calling getfriends (userwrapper. Getusername()) )Get the user's friends and save each friend by calling savefriend

Now I'm new to rxjava and don't know how to deal with lists I looked at a Jake Wharton's slides and found that he used a function flattenlist (), but I didn't know its definition If you can help combine this link, it will be huge

Update 1

This is where I'm now

mApiService.getUserSettings()
            .map(this::saveUser)
            .flatMap(userWrapper -> mApiService.getItems(userWrapper.getUserName()));
            .flatMapIterable( ? "How to iterate for each item" ? );

Update 2

I want 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))

Rxjava has added flatmapiterable So you don't need a flattenlist right now, for example,

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(...);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>