当给定参数不同时如何正确地继续任务 [英] How to properly continue tasks when a given parameter is different

查看:115
本文介绍了当给定参数不同时如何正确地继续任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对任务现在,我怀疑。在电子邮件/通行证注册后,我不得不更新用户的个人资料。所以我第一次尝试这个:

$ p $ FirebaseAuth.getInstance()。createUserWithEmailAndPassword(email,password);
.continueWithTask(new Continuation< AuthResult,Task< Void>>(){
@Override
public Task< Void> then(@NonNull Task< AuthResult> t)throws Exception { b $ b UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(fullname)
.build();
return t.getResult()。getUser()。updateProfile(profileUpdates);

$)
.addOnFailureListener(this,mOnSignInFailureListener)
.addOnSuccessListener(this,mOnSignInSuccessListener); //< - 问题!

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

  final Task< AuthResult>主要任务; 
mainTask = FirebaseAuth.getInstance()。createUserWithEmailAndPassword(email,password);
mainTask
.continueWithTask(new Continuation< AuthResult,Task< Void>>(){
@Override
public Task< Void> then(@NonNull Task< AuthResult> t )抛出异常{
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(全名)
.build();
返回t.getResult()。getUser()。 updateProfile(profileUpdates);
}
})
.continueWithTask(new Continuation< Void,Task< AuthResult>>(){
@Override
public Task< AuthResult>然后(@NonNull Task< Void> t)抛出异常{
return mainTask;
}
})
.addOnFailureListener(this,mOnSignInFailureListener)
.addOnSuccessListener (这个,mOnSignInSuccessListener);


解决方案

看起来你期待AuthResult得到直接传递给mOnSignInSuccessListener。在这种情况下,在我看来,这是不值得尝试强制一个额外的延期返回您正在寻找的价值。



而不是试图安排将AuthResult作为参数传递给该侦听器,侦听器可以直接到达mainTask.getResult(),或者可以将AuthResult保存到成员变量中并以此方式访问。无论哪种方式,这是安全的,因为mOnSignInSuccessListener只会在mainTask完成后调用,这确保了AuthResult准备就绪。

I'm facing tasks right now and I have doubts. 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);

解决方案

It looks like you're expecting an AuthResult to get passed directly to mOnSignInSuccessListener. In this particular case, in my opinion, it's not worthwhile to try to coerce an extra Continuation to return the value you're looking for.

Instead of trying to arrange for the AuthResult to be passed to that listener as a parameter, the listener can simply reach directly into mainTask.getResult(), or you can save the AuthResult in a member variable and access it that way. Either way, it's safe because the mOnSignInSuccessListener will only be invoked after mainTask completes, which ensures the AuthResult is ready.

这篇关于当给定参数不同时如何正确地继续任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆