This is actually a three-questions question.
I'm facing tasks right now and I have doubts.
(for reference: Task and Tasks documentation).
1.
What's the difference between task.continueWith()
and task.continueWithTask()
, can you provide an example for each one?
2. After an email/pass registration, I had to update the user's profile. So I first tried this:
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password);
.continueWithTask(new Continuation<AuthResult, Task<Void>>() {
@Override
public Task<Void> then(@NonNull Task<AuthResult> t) throws Exception {
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(fullname)
.build();
return t.getResult().getUser().updateProfile(profileUpdates);
}
})
.addOnFailureListener(this, mOnSignInFailureListener)
.addOnSuccessListener(this, mOnSignInSuccessListener); // <- problem!
The problem is in the last line my listener waits for an AuthResult
parameter but updateProfile
task sends a Void
. I handled that situation like bellow but it seems too messy. Tell me if there is another better way to do this:
final Task<AuthResult> mainTask;
mainTask = FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password);
mainTask
.continueWithTask(new Continuation<AuthResult, Task<Void>>() {
@Override
public Task<Void> then(@NonNull Task<AuthResult> t) throws Exception {
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(fullname)
.build();
return t.getResult().getUser().updateProfile(profileUpdates);
}
})
.continueWithTask(new Continuation<Void, Task<AuthResult>>() {
@Override
public Task<AuthResult> then(@NonNull Task<Void> t) throws Exception {
return mainTask;
}
})
.addOnFailureListener(this, mOnSignInFailureListener)
.addOnSuccessListener(this, mOnSignInSuccessListener);
3. I'd like to create custom tasks like these ones in firebase in order to chain my API async calls. How can I achieve that?
Thank you very much.