Action in RxJava chain not working as it should












0















I am new to RxJava and RxFirebase and I am making a register system for my app. All the steps in the registrations process need to happen in a sequence and this sequence needs to abort if a step fails.



This is code from my register UI class. I call register() on another class and subscribe on the return value, which is a Maybe.



        MyClass.register(username, email, password, photo_uri)
.subscribe(
a -> log(a.getUser().getDisplayName()),
error -> { }
)


Inside the register function I have my "chain":



    public Maybe<AuthResult> register(String username, String email, String pass, Uri photo_uri) {
return RxFirebaseAuth.createUserWithEmailAndPassword(mAuth, email, pass)
.doAfterSuccess(authResult -> setFirebaseUsername(authResult.getUser(), username))
.doAfterSuccess(authResult -> setFirestoreUsername(authResult.getUser(), username));
}


The problem is in the SetFirestoreUsername:



        public Completable setFirestoreUsername(FirebaseUser user, String username) {
HashMap<String, Object> profile_data = new HashMap<>();
profile_data.put("username", username);

DocumentReference document = FirebaseFirestore.getInstance().collection("users").document(user.getUid());

// Works
//RxFirestore.setDocument(document, profile_data).subscribe();

// Doesn't work
return RxFirestore.setDocument(document, profile_data);
}


If you take a look at the "works / doesn't work" comments, the difference between the piece of code that works and the code that doesn't is that the latter is returned and used in the subscription chain. I have verified that setFirestoreUsername gets called. The only difference is that when I use return the internal workings of RxFirestore.setDocument simply does not work but no errors are thrown.










share|improve this question

























  • After you returned return RxFirestore.setDocument(document, profile_data); Did you subscribe on the result? Because otherwise items will not be emitted.

    – GensaGames
    Nov 22 '18 at 10:52











  • Yes, the register function returns an Maybe that has in it a trigger to setFirestoreUsername. In the subscribe code I have shown is where I subscribe to that Maybe. Atleas that's how I understand it. If I place log statements inside setFireStoreUsername they will get displayed as well but the underlying setDocument just doesn't work this way

    – BFMC2
    Nov 22 '18 at 11:40













  • I don't see because of chunk of codes... In places, where you receiving this return RxFirestore.setDocument(document, profile_data); Do you have subscribe() method on that?

    – GensaGames
    Nov 22 '18 at 11:44













  • I improved my post a little bit. Yes, I call subscribe but not directly on RxFirestore.setDocument. This part is returned to the chain and it is on this chain that I subscribe in my register UI class. I have verified that the method gets called. It's just that if I directly call subscribe it works, if I make it part of my chain it doesn't work.

    – BFMC2
    Nov 22 '18 at 13:04
















0















I am new to RxJava and RxFirebase and I am making a register system for my app. All the steps in the registrations process need to happen in a sequence and this sequence needs to abort if a step fails.



This is code from my register UI class. I call register() on another class and subscribe on the return value, which is a Maybe.



        MyClass.register(username, email, password, photo_uri)
.subscribe(
a -> log(a.getUser().getDisplayName()),
error -> { }
)


Inside the register function I have my "chain":



    public Maybe<AuthResult> register(String username, String email, String pass, Uri photo_uri) {
return RxFirebaseAuth.createUserWithEmailAndPassword(mAuth, email, pass)
.doAfterSuccess(authResult -> setFirebaseUsername(authResult.getUser(), username))
.doAfterSuccess(authResult -> setFirestoreUsername(authResult.getUser(), username));
}


The problem is in the SetFirestoreUsername:



        public Completable setFirestoreUsername(FirebaseUser user, String username) {
HashMap<String, Object> profile_data = new HashMap<>();
profile_data.put("username", username);

DocumentReference document = FirebaseFirestore.getInstance().collection("users").document(user.getUid());

// Works
//RxFirestore.setDocument(document, profile_data).subscribe();

// Doesn't work
return RxFirestore.setDocument(document, profile_data);
}


If you take a look at the "works / doesn't work" comments, the difference between the piece of code that works and the code that doesn't is that the latter is returned and used in the subscription chain. I have verified that setFirestoreUsername gets called. The only difference is that when I use return the internal workings of RxFirestore.setDocument simply does not work but no errors are thrown.










share|improve this question

























  • After you returned return RxFirestore.setDocument(document, profile_data); Did you subscribe on the result? Because otherwise items will not be emitted.

    – GensaGames
    Nov 22 '18 at 10:52











  • Yes, the register function returns an Maybe that has in it a trigger to setFirestoreUsername. In the subscribe code I have shown is where I subscribe to that Maybe. Atleas that's how I understand it. If I place log statements inside setFireStoreUsername they will get displayed as well but the underlying setDocument just doesn't work this way

    – BFMC2
    Nov 22 '18 at 11:40













  • I don't see because of chunk of codes... In places, where you receiving this return RxFirestore.setDocument(document, profile_data); Do you have subscribe() method on that?

    – GensaGames
    Nov 22 '18 at 11:44













  • I improved my post a little bit. Yes, I call subscribe but not directly on RxFirestore.setDocument. This part is returned to the chain and it is on this chain that I subscribe in my register UI class. I have verified that the method gets called. It's just that if I directly call subscribe it works, if I make it part of my chain it doesn't work.

    – BFMC2
    Nov 22 '18 at 13:04














0












0








0








I am new to RxJava and RxFirebase and I am making a register system for my app. All the steps in the registrations process need to happen in a sequence and this sequence needs to abort if a step fails.



This is code from my register UI class. I call register() on another class and subscribe on the return value, which is a Maybe.



        MyClass.register(username, email, password, photo_uri)
.subscribe(
a -> log(a.getUser().getDisplayName()),
error -> { }
)


Inside the register function I have my "chain":



    public Maybe<AuthResult> register(String username, String email, String pass, Uri photo_uri) {
return RxFirebaseAuth.createUserWithEmailAndPassword(mAuth, email, pass)
.doAfterSuccess(authResult -> setFirebaseUsername(authResult.getUser(), username))
.doAfterSuccess(authResult -> setFirestoreUsername(authResult.getUser(), username));
}


The problem is in the SetFirestoreUsername:



        public Completable setFirestoreUsername(FirebaseUser user, String username) {
HashMap<String, Object> profile_data = new HashMap<>();
profile_data.put("username", username);

DocumentReference document = FirebaseFirestore.getInstance().collection("users").document(user.getUid());

// Works
//RxFirestore.setDocument(document, profile_data).subscribe();

// Doesn't work
return RxFirestore.setDocument(document, profile_data);
}


If you take a look at the "works / doesn't work" comments, the difference between the piece of code that works and the code that doesn't is that the latter is returned and used in the subscription chain. I have verified that setFirestoreUsername gets called. The only difference is that when I use return the internal workings of RxFirestore.setDocument simply does not work but no errors are thrown.










share|improve this question
















I am new to RxJava and RxFirebase and I am making a register system for my app. All the steps in the registrations process need to happen in a sequence and this sequence needs to abort if a step fails.



This is code from my register UI class. I call register() on another class and subscribe on the return value, which is a Maybe.



        MyClass.register(username, email, password, photo_uri)
.subscribe(
a -> log(a.getUser().getDisplayName()),
error -> { }
)


Inside the register function I have my "chain":



    public Maybe<AuthResult> register(String username, String email, String pass, Uri photo_uri) {
return RxFirebaseAuth.createUserWithEmailAndPassword(mAuth, email, pass)
.doAfterSuccess(authResult -> setFirebaseUsername(authResult.getUser(), username))
.doAfterSuccess(authResult -> setFirestoreUsername(authResult.getUser(), username));
}


The problem is in the SetFirestoreUsername:



        public Completable setFirestoreUsername(FirebaseUser user, String username) {
HashMap<String, Object> profile_data = new HashMap<>();
profile_data.put("username", username);

DocumentReference document = FirebaseFirestore.getInstance().collection("users").document(user.getUid());

// Works
//RxFirestore.setDocument(document, profile_data).subscribe();

// Doesn't work
return RxFirestore.setDocument(document, profile_data);
}


If you take a look at the "works / doesn't work" comments, the difference between the piece of code that works and the code that doesn't is that the latter is returned and used in the subscription chain. I have verified that setFirestoreUsername gets called. The only difference is that when I use return the internal workings of RxFirestore.setDocument simply does not work but no errors are thrown.







android firebase google-cloud-firestore rx-java chain






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 15:22









Frank van Puffelen

230k28376400




230k28376400










asked Nov 22 '18 at 10:28









BFMC2BFMC2

45114




45114













  • After you returned return RxFirestore.setDocument(document, profile_data); Did you subscribe on the result? Because otherwise items will not be emitted.

    – GensaGames
    Nov 22 '18 at 10:52











  • Yes, the register function returns an Maybe that has in it a trigger to setFirestoreUsername. In the subscribe code I have shown is where I subscribe to that Maybe. Atleas that's how I understand it. If I place log statements inside setFireStoreUsername they will get displayed as well but the underlying setDocument just doesn't work this way

    – BFMC2
    Nov 22 '18 at 11:40













  • I don't see because of chunk of codes... In places, where you receiving this return RxFirestore.setDocument(document, profile_data); Do you have subscribe() method on that?

    – GensaGames
    Nov 22 '18 at 11:44













  • I improved my post a little bit. Yes, I call subscribe but not directly on RxFirestore.setDocument. This part is returned to the chain and it is on this chain that I subscribe in my register UI class. I have verified that the method gets called. It's just that if I directly call subscribe it works, if I make it part of my chain it doesn't work.

    – BFMC2
    Nov 22 '18 at 13:04



















  • After you returned return RxFirestore.setDocument(document, profile_data); Did you subscribe on the result? Because otherwise items will not be emitted.

    – GensaGames
    Nov 22 '18 at 10:52











  • Yes, the register function returns an Maybe that has in it a trigger to setFirestoreUsername. In the subscribe code I have shown is where I subscribe to that Maybe. Atleas that's how I understand it. If I place log statements inside setFireStoreUsername they will get displayed as well but the underlying setDocument just doesn't work this way

    – BFMC2
    Nov 22 '18 at 11:40













  • I don't see because of chunk of codes... In places, where you receiving this return RxFirestore.setDocument(document, profile_data); Do you have subscribe() method on that?

    – GensaGames
    Nov 22 '18 at 11:44













  • I improved my post a little bit. Yes, I call subscribe but not directly on RxFirestore.setDocument. This part is returned to the chain and it is on this chain that I subscribe in my register UI class. I have verified that the method gets called. It's just that if I directly call subscribe it works, if I make it part of my chain it doesn't work.

    – BFMC2
    Nov 22 '18 at 13:04

















After you returned return RxFirestore.setDocument(document, profile_data); Did you subscribe on the result? Because otherwise items will not be emitted.

– GensaGames
Nov 22 '18 at 10:52





After you returned return RxFirestore.setDocument(document, profile_data); Did you subscribe on the result? Because otherwise items will not be emitted.

– GensaGames
Nov 22 '18 at 10:52













Yes, the register function returns an Maybe that has in it a trigger to setFirestoreUsername. In the subscribe code I have shown is where I subscribe to that Maybe. Atleas that's how I understand it. If I place log statements inside setFireStoreUsername they will get displayed as well but the underlying setDocument just doesn't work this way

– BFMC2
Nov 22 '18 at 11:40







Yes, the register function returns an Maybe that has in it a trigger to setFirestoreUsername. In the subscribe code I have shown is where I subscribe to that Maybe. Atleas that's how I understand it. If I place log statements inside setFireStoreUsername they will get displayed as well but the underlying setDocument just doesn't work this way

– BFMC2
Nov 22 '18 at 11:40















I don't see because of chunk of codes... In places, where you receiving this return RxFirestore.setDocument(document, profile_data); Do you have subscribe() method on that?

– GensaGames
Nov 22 '18 at 11:44







I don't see because of chunk of codes... In places, where you receiving this return RxFirestore.setDocument(document, profile_data); Do you have subscribe() method on that?

– GensaGames
Nov 22 '18 at 11:44















I improved my post a little bit. Yes, I call subscribe but not directly on RxFirestore.setDocument. This part is returned to the chain and it is on this chain that I subscribe in my register UI class. I have verified that the method gets called. It's just that if I directly call subscribe it works, if I make it part of my chain it doesn't work.

– BFMC2
Nov 22 '18 at 13:04





I improved my post a little bit. Yes, I call subscribe but not directly on RxFirestore.setDocument. This part is returned to the chain and it is on this chain that I subscribe in my register UI class. I have verified that the method gets called. It's just that if I directly call subscribe it works, if I make it part of my chain it doesn't work.

– BFMC2
Nov 22 '18 at 13:04












1 Answer
1






active

oldest

votes


















0














The doAfterSuccess() operator is called with the value flowing through the pipeline, in your case setFirestoreUsername(authResult.getUser(), username). This is just a method call and the result is not used as part of the observer chain.



Since it is not part of the observer chain, there is no subscription. You may need to use the flatMapCompletable() operator instead of doAfterSuccess().






share|improve this answer
























  • Thanks, after reading this comment I found more on doAfterSuccess and it suggest the same. I settled for Maybe.flatMap(mymethod).andThen(Maybe.just(valueForNextFlatmap) etc and it works decent.

    – BFMC2
    Nov 23 '18 at 16:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428863%2faction-in-rxjava-chain-not-working-as-it-should%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The doAfterSuccess() operator is called with the value flowing through the pipeline, in your case setFirestoreUsername(authResult.getUser(), username). This is just a method call and the result is not used as part of the observer chain.



Since it is not part of the observer chain, there is no subscription. You may need to use the flatMapCompletable() operator instead of doAfterSuccess().






share|improve this answer
























  • Thanks, after reading this comment I found more on doAfterSuccess and it suggest the same. I settled for Maybe.flatMap(mymethod).andThen(Maybe.just(valueForNextFlatmap) etc and it works decent.

    – BFMC2
    Nov 23 '18 at 16:11
















0














The doAfterSuccess() operator is called with the value flowing through the pipeline, in your case setFirestoreUsername(authResult.getUser(), username). This is just a method call and the result is not used as part of the observer chain.



Since it is not part of the observer chain, there is no subscription. You may need to use the flatMapCompletable() operator instead of doAfterSuccess().






share|improve this answer
























  • Thanks, after reading this comment I found more on doAfterSuccess and it suggest the same. I settled for Maybe.flatMap(mymethod).andThen(Maybe.just(valueForNextFlatmap) etc and it works decent.

    – BFMC2
    Nov 23 '18 at 16:11














0












0








0







The doAfterSuccess() operator is called with the value flowing through the pipeline, in your case setFirestoreUsername(authResult.getUser(), username). This is just a method call and the result is not used as part of the observer chain.



Since it is not part of the observer chain, there is no subscription. You may need to use the flatMapCompletable() operator instead of doAfterSuccess().






share|improve this answer













The doAfterSuccess() operator is called with the value flowing through the pipeline, in your case setFirestoreUsername(authResult.getUser(), username). This is just a method call and the result is not used as part of the observer chain.



Since it is not part of the observer chain, there is no subscription. You may need to use the flatMapCompletable() operator instead of doAfterSuccess().







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 17:40









Bob DalgleishBob Dalgleish

6,0062233




6,0062233













  • Thanks, after reading this comment I found more on doAfterSuccess and it suggest the same. I settled for Maybe.flatMap(mymethod).andThen(Maybe.just(valueForNextFlatmap) etc and it works decent.

    – BFMC2
    Nov 23 '18 at 16:11



















  • Thanks, after reading this comment I found more on doAfterSuccess and it suggest the same. I settled for Maybe.flatMap(mymethod).andThen(Maybe.just(valueForNextFlatmap) etc and it works decent.

    – BFMC2
    Nov 23 '18 at 16:11

















Thanks, after reading this comment I found more on doAfterSuccess and it suggest the same. I settled for Maybe.flatMap(mymethod).andThen(Maybe.just(valueForNextFlatmap) etc and it works decent.

– BFMC2
Nov 23 '18 at 16:11





Thanks, after reading this comment I found more on doAfterSuccess and it suggest the same. I settled for Maybe.flatMap(mymethod).andThen(Maybe.just(valueForNextFlatmap) etc and it works decent.

– BFMC2
Nov 23 '18 at 16:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428863%2faction-in-rxjava-chain-not-working-as-it-should%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python