简体   繁体   中英

RxAndroid Observable Populate Sub Lists

So I realise the title is a bit vague, but the problem I have is quite complicated (for me at least).

I have an Observable that loads a list from an API. The user response resembles this:

{
    "status": "Successful",
    "count": 2,
    "response": [{
        "_id": "abc",
        "email": "email@email.com",
        "surname": "Surname",
        "firstname": "Firstname",
        "posts": [
            "123",
            "456",
            "789"]
       }]
    ,...
}

and then for the posts response:

{
    "status": "Successful",
    "count": 7,
    "response": [{
            "_id": "123",
            "name": "Post 1"
        }, {
            "_id": "456",
            "name": "Post 2"
        }, {
            "_id": "789",
            "name": "post 3"
        }]
}

I already have a method that returns a Observable<Post> that maps the response correctly.

Now the problem I have: I can fetch a list of users, with the list of posts id's, but I want to populate that list with the actual objects that I can query from my method ( Observable<Post> getPost(String id))

I've tried all sorts of approaches, but nothing works.

So far, I have the following to map the response to a List<User> - and I want to, for each user in the list, get the Posts associated with the ID in the List<String :

public Observable<List<User>> getUsers() {
    return apiService.getUsers()
            .flatMap(userResponse -> Observable.from(userResponse.users).toList())
            .flatMap(user -> Observable.from(users))
            .toList();
}

The above emits a List<User> , which is what I need. The problem I keep running into, is that it emits each Post item, and eventually just returns 3 User items, all the same (the first user). So how to I perform some transformations to get the posts for each item in the list, and apply it to another list within the model that represents the actual post?

Thanks, and sorry for the confusion - I'm new to RxJava so its taking a while to wrap my head around it all.

EDIT: Solved - check answer below

Ok, finally managed to solve it. For those interested, heres the solution: I had a method to get the list of posts for the user:

public Observable<List<Posts>> getPostsForUser(User user) {
    return Observable
            .from(user.posts)
            .flatMap(this::getPost)
            .toList();
}

Then used that when getting the list of users as follows:

public Observable<List<User>> getPopulatedUser() {
    return apiService.getUsers()
            .flatMap(userResponse -> Observable.from(userResponse.users).toList())
            .flatMap(Observable::from)
            .flatMap(user -> getPostsForUser(user)
                    .map(posts -> {
                        user.setPosts(posts);
                        return (User) user;
                    })
            ).toList();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM