0

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.

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;

}
ankuranurag2
  • 2,300
  • 15
  • 30
vander ouana
  • 49
  • 2
  • 3

3 Answers3

1

I once had almost the same problem as you. What I did is to replace OnCompleteListenerwith addOnSuccessListener, and everything got solved.

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.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
0

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. 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.

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. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

Got the same issue, turns out I was changing activity before onComplete ends. If you are changing activity (for example go to Dashboard activity after signing success), make sure to call startActivity(intent) inside onComplete task Successful)

kenny
  • 1