有关任务的所有信息:continueWith与continueWithTask等

Fab*_*cio 7 android firebase

这实际上是三个问题。

我现在正面临任务,我对此表示怀疑。

(供参考:任务任务文档)。


1.task.continueWith()和 之间有什么区别task.continueWithTask(),您能为每个例子提供一个例子吗?


2. 注册电子邮件/通行证后,我必须更新用户的个人资料。所以我首先尝试了这个:

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!
Run Code Online (Sandbox Code Playgroud)

问题出在最后一行,我的侦听器等待一个AuthResult参数,但updateProfile任务发送一个Void。我像波纹管一样处理这种情况,但似乎太混乱了。告诉我是否还有另一种更好的方法可以做到这一点:

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);
Run Code Online (Sandbox Code Playgroud)

3. 我想在firebase中创建诸如此类的自定义任务,以链接我的API异步调用。我该如何实现?


非常感谢你。