I have started learning about RxJava2 and I'd like to know why when I am using a MaybeEmitter object it's always disposed after onSuccess method is called. So any changes on the Profile object that comes from Firebase (the listener onDataChange is called) are not passed to my Presenter as e.onSuccess(profile) is called for the second time, but nothing happen**. Is there a way to keep receiving updates even after onComplete or OnSuccess method is called?
My Presenter:
@Override
public void loadUserProfileData(String userUid) {
getCompositeDisposable().add(
getDatabaseSource().getProfile(userUid)
.subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.subscribeWith(new DisposableMaybeObserver<Profile>() {
@Override
public void onComplete() {
getView().goToLoginActivity();
}
@Override
public void onSuccess(Profile profile) {
getView().setUpProfileFields(profile);
}
@Override
public void onError(Throwable e) {
getView().showMessage(e.getLocalizedMessage());
}
})
);
}
My FirebaseDatabaseService:
@Override
public Maybe<Profile> getProfile(final String uid) {
return Maybe.create(
new MaybeOnSubscribe<Profile>() {
@Override
public void subscribe(final MaybeEmitter<Profile> e) throws Exception {
final DatabaseReference dataBaseProfile =
FirebaseDatabase.getInstance().getReference(USER_PROFILE).child(uid);
dataBaseProfile.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
Profile profile = null;
// There's always only one Profile object associated to each user
for(DataSnapshot dataSnapshot: snapshot.getChildren()){
profile = dataSnapshot.getValue(Profile.class);
}
e.onSuccess(profile);
} else {
e.onComplete();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("FIREBASE", databaseError.toString());
e.onError(databaseError.toException());
}
});
}
}
);
}
because you are using
Maybe
useObservable
instead.here are the different RXjava2 Observers.
Emits 0 or n items and terminates with a success or an error event.
Emits either a single item or an error event. The reactive version of a method call.
Succeeds with an item, or no item, or errors. The reactive version of an Optional.
Either complete with success with an error event. It never emits items. The reactive version of a Runnable.
read about RxJava here