Reputation: 3331
I'm not getting any of the callbacks for user creation using createUserwithEmailAndPassword()
. I'm probably missing something obvious here, but I can't seem to figure out what.
I've enabled the username and password option from the Firebase Console, the user is added to the Firebase users' records, however none of the listeners are firing.
Here's the code where I create the user.
firebaseAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth)
{
// this is not called.
synchronized (RegisterUser.this) {
RegisterUser.this.notify();
}
}
});
Task<AuthResult> taskResult = firebaseAuth.createUserWithEmailAndPassword(model.getEmailId().getValue(),
model.getPassword().getValue())
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
// this never gets called.
if (task.isSuccessful()) {
synchronized (RegisterUser.this) {
RegisterUser.this.notify();
}
}
else {
Logger.e(TAG, "Unable to register user.");
}
}
});
taskResult.isSuccessful(); // returns false makes sense because it's a network call.
synchronized (RegisterUser.this) {
RegisterUser.this.wait();
}
I've tried adding both OnSuccessListener
and OnFailureListener
too, but they too are never called.
Edit :
So, realized my mistake thanks to @DougStevensen's answer down there. I had multiple threads running, turns out I had a wait()
statement in one of them, which was blocking the callback even though I had passed the Executor
thinking the callback would be from the Executor
provided thread.
Upvotes: 0
Views: 208
Reputation: 317467
It looks like you are blocking the main thread:
synchronized (RegisterUser.this) {
RegisterUser.this.wait();
}
In the hope that the callback will unblock it:
if (task.isSuccessful()) {
synchronized (RegisterUser.this) {
RegisterUser.this.notify();
}
}
This is not going to work. First of all, on Android, you should never block the main thread. That will cause your application to hang and eventually ANR. Second, the Task asynchronous callbacks all happen on the main thread by default. So, if you block the main thread, the callbacks will never happen.
Instead of blocking the main thread with java synchronization primitives, you should take up proper Android asynchronous programming practices. The first thing to do is remove all those synchronized
blocks so you can see the callback getting invoked.
Upvotes: 1