为什么当我使用setDisplayName()时,结果有时为null有时不是null?

时间:2018-12-15 03:46:43

标签: android firebase firebase-authentication

我想在创建用户时使用setDisplayName()来使用getDisplayName()在我的Firebase数据库中进行设置。 这是我的github:https://github.com/4dasqaw/GrowthWatcher

ProfilActivity:

public class ProfilActivity extends BaseActivity {

    private static final int UPDATE_USERNAME = 30;
    private FirebaseAuth mAuth;
    private TextView mUsername;
    private EditText mNewUsername;
    private ProgressBar mProgressBar;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAuth = FirebaseAuth.getInstance();
        setContentView(R.layout.activity_profil);

        mUsername = findViewById(R.id.profile_tv_username);
        mNewUsername = findViewById(R.id.profile_et_username);
        mProgressBar = findViewById(R.id.profile_progressbar);
        mButton = findViewById(R.id.profile_change_username_btn);
        mProgressBar.setVisibility(View.INVISIBLE);

        this.updateUIWhenCreating();

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mProgressBar.setVisibility(View.VISIBLE);
                updateUsernameInFirebase();
            }
        });
    }

    private void updateUIWhenCreating(){

        if (this.getCurrentUser() != null){
            String username = TextUtils.isEmpty(this.getCurrentUser().getDisplayName()) ? getString(R.string.info_no_username_found) : this.getCurrentUser().getDisplayName();
            this.mUsername.setText(username); //Username is correct here
        }
        UserHelper.getUser(this.getCurrentUser().getUid()).addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                User currentUser = documentSnapshot.toObject(User.class);
                String username = TextUtils.isEmpty(currentUser.getUsername()) ? getString(R.string.info_no_username_found) : currentUser.getUsername(); //don't work, username is empty because currentUser is null
            }
        });
    }

    private void updateUsernameInFirebase(){

        String username = this.mNewUsername.getText().toString();

        if (this.getCurrentUser() != null){
            if (!username.isEmpty() &&  !username.equals(getString(R.string.info_no_username_found))){
                UserHelper.updateUsername(username, this.getCurrentUser().getUid()).addOnFailureListener(this.onFailureListener()).addOnSuccessListener(this.updateUIAfterRESTRequestsCompleted(UPDATE_USERNAME));
            }
        }
    }

    private OnSuccessListener<Void> updateUIAfterRESTRequestsCompleted(final int origin){
        return new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                switch (origin){
                    // 8 - Hiding Progress bar after request completed
                    case UPDATE_USERNAME:
                        mProgressBar.setVisibility(View.INVISIBLE);
                        break;
                }
            }
        };
    }


}

我的MainActivity:

public class MainActivity extends BaseActivity {

    private final static int RC_SIGN_IN = 100;

    private FirebaseAuth mAuth;
    private TextView mMail, mId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mAuth = FirebaseAuth.getInstance();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMail = findViewById(R.id.main_mail);
        mId = findViewById(R.id.main_id);

  @Override
    public void onStart() {
        super.onStart();
        String vEmail = this.getCurrentUser().getEmail();
        String vId = this.getCurrentUser().getDisplayName(); //show nothing
        mMail.setText(vEmail);
        mId.setText(vId);
    }

}

我的UserHelp类:

package fr.tom_d.growthwatcher.api;

public class UserHelper {

    private static final String COLLECTION_NAME = "users";

    public static CollectionReference getUsersCollection() {
        return FirebaseFirestore.getInstance().collection(COLLECTION_NAME);
    }

    public static Task<Void> createUser(String uid, String username) {
        User userToCreate = new User(uid, username);
        return UserHelper.getUsersCollection().document(uid).set(userToCreate);
    }

    public static Task<DocumentSnapshot> getUser(String uid) {
        return UserHelper.getUsersCollection().document(uid).get();
    }

    public static Task<Void> updateUsername(String username, String uid) {
        return UserHelper.getUsersCollection().document(uid).update("username", username);
    }
}

我的BaseActivity抽象类:

public abstract class BaseActivity extends AppCompatActivity {

    protected FirebaseUser getCurrentUser(){ return FirebaseAuth.getInstance().getCurrentUser(); }

    protected void handleResponseAfterSignIn(int requestCode, int resultCode, Intent data){

        if (requestCode == RC_SIGN_IN) {
            if (resultCode == RESULT_OK) {
                this.createUserInFirestore();
            } else {
            }
        }
    }

    protected void createUserInFirestore(){

        if (this.getCurrentUser() != null){

            String username = this.getCurrentUser().getDisplayName();
            String uid = this.getCurrentUser().getUid();

            UserHelper.createUser(uid, username).addOnFailureListener(this.onFailureListener());
        }
    }
}

我的updateUser()方法解决了问题,但我不明白为什么我的createUser()不能... 另外,如果不是数据库的情况(不调用updateUser()),但不是MainActivity中的vId,则ProfilActivity中的mUsername也将获取正确的用户名。 我不明白我的错在哪里,有人可以解释一下吗?

0 个答案:

没有答案