Search code examples
androidrx-javabehaviorsubject

Concat Observables to BehaviourSubject


I have a BehaviourSubject in my ViewModel that i would like to concat to it observables that do actions. So, i want get a list of users from the DB. I have an observable that has this operation. I subscribe to the BehaviourSubject in my view and then call getUsers, but with no result. Thanks in advance.

Code: ViewModel

public Observable<S> getState(Observable<S> input) {
    return state.concatMap(o -> input);
}

@Override
public Observable<UserListState> getUsers() {
    getState(dataUseCase.getUserList());
}

View:

userListVM.getState().compose(bindToLifecycle())
            .subscribe(new Subscriber<>() {
     void onNext(Object o){
     Log.d("onNext", o.toString()); // null !!
     }
});
userListVM.getUsers();

Solution

  • This will do nothing, as you're not subscribing to the userListVM.getUsers() Observable.

    Your code isn't exact, as you have getState with 0 and 1 parameters. Instead, perhaps you should consider something like:

    userListVM.getUsers().subscribe(stateSubject::onNext);
    

    This will propagate the items from the observable to the subject, but you will need to handle unsubscription manually/