5
\$\begingroup\$

I have a service with these methods:

Observable<User> getUsers();
Observable<Role> getUserRoles(String userId);

For each user, I want to get her roles and set them.

I have this method working:

private Observable<User> getUsersWithRoles() {
        return service.getUsers().flatMap(new Func1<User, Observable<User>>() {
            @Override
            public Observable<User> call(User user) {
                return Observable.combineLatest(Observable.just(user), service.getUserRoles(user).toList(), new Func2<User, List<Role>, User>() {
                    @Override
                    public User call(User user, List<Role> roles) {
                        user.setRoles(roles);
                        return user;
                    }
                });
            }
        });
    }

I don't like these parts so much:

  • Observable.just(user)
  • service.getUserRoles(user).toList()

Is there a cleaner way to build this Observable?

\$\endgroup\$
  • \$\begingroup\$ which rx-java version you are using? \$\endgroup\$ – dkb Dec 21 '18 at 7:46
  • \$\begingroup\$ Some more ways to create observable: github.com/ReactiveX/RxJava/wiki/Creating-Observables#create \$\endgroup\$ – dkb Dec 21 '18 at 7:47
  • \$\begingroup\$ any particular reason why you do not like it and wanted to change it? \$\endgroup\$ – dkb Dec 21 '18 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.