0

The following Java code retrieves clothes data from a Firebase database. However, the Log.d(tag, "moved") and Log.d(tag, "signed in") lines don't print any messages and the main Activity is frozen.

public ArrayList<Clothe> getAllClothes() throws InterruptedException {
    synchronized (ClotheFirebaseHelper.this) {
        mAuth.signInWithEmailAndPassword("#########@gmail.com","**************").addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                ClotheFirebaseHelper.this.notifyAll();
                Log.d("tag","signed in");
            }
        });
        ClotheFirebaseHelper.this.wait();
    }
    Log.d("tag","moved");
    String uid = Objects.requireNonNull(mAuth.getCurrentUser()).getUid();
    Task<DataSnapshot> task = reference.child(uid).get();
    synchronized (ClotheFirebaseHelper.this) {
        task.onSuccessTask(new SuccessContinuation<DataSnapshot, Object>() {
            @NonNull
            @Override
            public Task<Object> then(DataSnapshot dataSnapshot) throws Exception {
                ClotheFirebaseHelper.this.notifyAll();
                return null;
            }
        });
        ClotheFirebaseHelper.this.wait();
    }
    Log.d("tag","ready?");
    Log.d("tag",task.getResult().toString());
    return null;
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 2
    Firebase provides non-blocking asynchronous APIs. You should not force your code to block until an operation is complete, otherwise your app could crash with an ANR. Only the async callbacks can tell you when an operation is complete. – Doug Stevenson Jun 16 '23 at 14:01
  • Agreed with Doug here. I recommend checking out my answers on https://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener and https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519. – Frank van Puffelen Jun 16 '23 at 16:19
  • Agreed with Doug too. If you understand Kotlin, this [resource](https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-email-and-password-in-jetpack-compose-bd70ca56ea91) will help. Here is the corresponding [repo](https://github.com/alexmamo/FirebaseSignInWithEmailAndPassword). – Alex Mamo Jun 18 '23 at 08:40

0 Answers0