简体   繁体   English

创建了Firebase Auth帐户,但从未调用完整的侦听器

[英]Firebase Auth account created but on complete listener never called

I have this method in my main activity where I can only move on after it is confirmed that the user account was successfully created. 我的主要活动中有此方法,只有在确认用户帐户已成功创建后,我才能继续操作。 when I click on confirm I can see that the account is created on firebase but onCompleteListener seems to never be invoked, so my count down latch is never decreased in value. 当我单击确认时,我可以看到该帐户是在firebase上创建的,但是似乎从未调用过onCompleteListener,因此我的倒计时闩锁的价值从未降低。

public boolean signUpUser(User user)
{

    FirebaseApp.initializeApp(this);



    FirebaseAuth firebaseAuth=FirebaseAuth.getInstance();
    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();


   // success = false;

    CountDownLatch countDownLatch = new CountDownLatch(1);

    Log.e("Account","Creating user");

    firebaseAuth.createUserWithEmailAndPassword(user.getEmail(),user.getPassword()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
             @Override
             public void onComplete(Task<AuthResult> task) {


                 Log.e("Account","User Created");
                 if(task.isSuccessful())
                 {
                     //createProfile(user,task);
                     success = true;
                 }


                 countDownLatch.countDown();
             }



         });


    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return success;

}

I once had almost the same problem as you. 我曾经遇到过与您几乎相同的问题。 What I did is to replace OnCompleteListener with addOnSuccessListener , and everything got solved. 我所做的就是更换OnCompleteListeneraddOnSuccessListener ,一切都得到有效解决。

Try to use this code : 尝试使用此代码:

firebaseAuth.signInWithEmailAndPassword(user.getEmail(),user.getPassword())
      .addOnSuccessListener(LoginActivity.this, new OnSuccessListener<AuthResult>(){

                @Override
                public void onsuccess(@NonNull AuthResult authResult){
                     //try to run something in here
                }
             });

Hope that this will solve your problem as well. 希望这也能解决您的问题。

You cannot return something now that hasn't been loaded yet. 您现在无法返回尚未加载的内容。 With other words, you cannot simply return your success boolean variable outside the onComplete() method because it will always hold the default value of false , due the asynchronous behaviour of this method. 换句话说,您不能简单地在onComplete()方法之外返回success布尔值变量,因为由于此方法的异步行为,它将始终保持默认值false This means that by the time you are trying to return that result, the data hasn't finished loading yet from the database and that's why is not accessible. 这意味着,当您尝试返回该结果时,数据尚未从数据库中完成加载,因此无法访问。 That's why is holding the initial value of false and not true as you expected. 这就是为什么要保留false不是您期望的true的初始值的原因。

A quick solve for this problem would be to use the logic related to your success variable only inside the onComplete() method, otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. 一个快速的解决这个问题是使用与您的逻辑success只是内部变量onComplete()方法,否则我建议你从这个看我anwser的最后部分职位中,我已经解释了如何可以做到用自定义回调。 You can also take a look at this video for a better understanding. 您也可以观看此视频 ,以更好地理解。

Got the same issue, turns out I was changing activity before onComplete ends. 遇到了同样的问题,原来我是在onComplete结束之前更改活动。 If you are changing activity (for example go to Dashboard activity after signing success), make sure to call startActivity(intent) inside onComplete task Successful) 如果要更改活动(例如,签名成功后转到仪表板活动),请确保在onComplete任务成功内调用startActivity(intent)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否真的需要删除完整的侦听器Firebase身份验证? - Is it really necessary to remove the complete listener Firebase Authentication? Firebase onComplete 从未在 Android 中调用 10 - Firebase onComplete never called in Android 10 Google 登录 Firebase 身份验证,显示“选择帐户” - Google sign in Firebase auth, show “choose account” 尝试创建帐户时发生 Firebase 身份验证错误 - Firebase Auth Error While Trying To Create An Account 谷歌探戈更新监听器onXyzIjAvailable回调永远不会被调用 - Google Tango Update Listener onXyzIjAvailable Callback never gets called 用Spring-Factory创建的ENUM bean,但从未调用@PostConstruct - ENUM bean created with a Spring-Factory but the @PostConstruct is never called 如何使用Firebase Auth登出您的Android Studio帐户? - How to log out of your android studio account using Firebase Auth? 创建帐户后,在 firebase 函数中获取用户的显示名称 - Get user's displayname in firebase function once account is created 已跳过侦听器中的 Firebase 侦听器 - Firebase listener in listener skipped Firebase Java SDK不会保留数据,也不会触发回调。 任务似乎从未完成 - Firebase Java SDK doesn't persist data and doesn't trigger callback. Tasks seems never complete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM