12

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.

Fabricio
  • 7,705
  • 9
  • 52
  • 87
  • 6
    If you are interested in Tasks API, I recommend you to read through this awesome blog series **Become a Firebase Taskmaster!** by Doug Stevenson, [start from here (part one)](https://firebase.googleblog.com/2016/09/become-a-firebase-taskmaster-part-1.html) – Wilik Oct 20 '16 at 16:14
  • @Fabricio These are great questions! Would you mind splitting them up into different questions here on stack overflow so they're easier for others to search for and find? I'll be happy to answer all of them - just let me know when they're available. – Doug Stevenson Oct 20 '16 at 16:55
  • @DougStevenson Here you go: (1)http://stackoverflow.com/questions/40161333/continuewith-vs-continuewithtask (2) http://stackoverflow.com/questions/40161354/how-to-create-custom-tasks (3) http://stackoverflow.com/questions/40161374/how-to-properly-continue-tasks-when-a-given-parameter-is-different – Fabricio Oct 20 '16 at 17:58

0 Answers0