0

I have this code and I want to know if this is possible with RxJava:

  • Function queries for a User from a server (async)
  • The server returns a User JSON object with a list of ID's of associated UserProfile(s)
  • Then for each of this ID, it needs to fetch the UserProfile given the ID (async also)
  • For each asynchronously fetched UserProfile append it to the User object, below is my pseudo-code.
  • I cannot use any blocking code, all request should be async.

Here's the code:

@Override
public Single<User> retrieve(String entityId) {
    BaasUser baasUser = new BaasUser();
    baasUser.setEntityId(entityId);
    baasUser.setIncludes(Arrays.asList("userProfile"));
    return baasUser.retrieve().map(user -> {
        String username = user.getUsername();
        String dateCreated = user.getDateCreated();
        String dateUpdated = user.getDateUpdated();

        List<UserProfile> userProfiles = new LinkedList<>();

        BaasLink userProfileLink = user.getFirstLink();
        userProfileLink.getEntities().forEach(stubEntity -> {
            Single<UserProfile> observable = stubEntity.retrieve().map(entity -> {
                UserProfile userProfile = new UserProfile();
                userProfile.setEntityId(entity.getStringProperty("entityId"));
                userProfile.setName(entity.getStringProperty("name"));
                return userProfile;
            });
            observable.subscribe(userProfile -> {
                // until all UserProfile is fetched this 'retrieve' "callback" should not return
                userProfiles.add(userProfile);
            }, error -> {
               // err
            });
        });
        User user1 = new User();
        user1.setDateCreated(dateCreated);  
        user1.setDateUpdated(dateUpdated);  
        user1.setUsername(username);
        user1.setUserProfiles(userProfiles);
        return user1;
    });
}

1 Answer 1

1

Here you have en example how to do your jobe maybe there is any typeerror becouse i dont have your objects

    Single.just("user")
            .observeOn(Schedulers.io())
            .flatMap(user -> Observable.zip(getCurrentUserData(user),getUserProfiles(user),(t1, t2) -> {
                //first func will return user eith some data next function wll return List<UserProfile> userProfiles
                return newuser;
            }))
            .subscribeOn(Schedulers.io())
}

private Single<List<UserProfile>> getUserProfiles(User user) {
    Observable.fromIterable( user.getFirstLink().getEntities())
            .flatMap(stubEntity ->stubEntity.retrieve())
            .map(o -> {
                //set user profile data
                return userprofile
            })
            .toList();


}

private Single<User> getCurrentUserData(User user) {
    Observable.just(user)
            .map(s -> {
                //set data
                return user;
            })
}
Sign up to request clarification or add additional context in comments.

Comments

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.