Problems with Firestore connections executing threads

前端 未结 3 684
旧时难觅i
旧时难觅i 2020-12-11 13:54

First of all, I would like to apologize if the title is misleading. English is not my native language and I wasn\'t sure how to name this post. Now the question:

I

相关标签:
3条回答
  • 2020-12-11 14:24

    Regarding the final reference, if the variable belongs to a class rather than being declared in the method, it need not be declared final.

    0 讨论(0)
  • 2020-12-11 14:24

    You're right guessing that, when using the value of your aa variable outside the onComplete() method, the data hasn't finished loading yet from the database and that's why it's not accessible. So this variable will aways hold the initial value of false.

    So in order to solve this, you need to wait for it. This can be done in two ways. The first solution would be a very quick solution that implies you to use the value of your aa variable only inside the onComplete() method and it will work perfectly fine. The second one is, if you want to use it outside the onComplete() method, I recommend you to 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.

    0 讨论(0)
  • 2020-12-11 14:41

    The problem is not so much caused by multi-thread, as by the fact that data is loaded from Firebase asynchronously. By the time your updateUI function looks at the value of aa, the onComplete hasn't run yet.

    This is easiest to see by placing a few well placed logging statements:

    System.out.println("Before attaching listener");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            System.out.println("Got document");
        }
    });
    System.out.println("After attaching listener");
    

    When you run this code it prints

    Before attaching listener

    After attaching listener

    Got document

    This is probably not what you expected, but it explains precisely why aa is unmodified when updateUI checks it. The document hasn't been read from Firestore yet, so onComplete hasn't run yet.

    The solution for this is to move all code that requires data from the database into the onComplete method. The simplest way in your case is:

    public void userExists(String uid) {
      FirebaseFirestore db = FirebaseFirestore.getInstance();
      DocumentReference docRef = db.collection("users").document(uid);
      docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot documentSnapshot = task.getResult();
                if (documentSnapshot.exists()) {
                    //Fill layout with the user data and the user linked document data
        
                    //USER DATA
                    txvNombre=findViewById(R.id.nombrePerfil);
                    txvNombre.setText(user.getDisplayName());
                    imvAvatar=findViewById(R.id.imvVistaPerfilAvatar);
                    Picasso.with(VistaPerfilActivity.this)
                            .load(user.getPhotoUrl())
                            .resize(500,500)
                            .centerCrop()
                            .into(imvAvatar);
        
        
                    //HERE GOES THE DOCUMENT DATA
    
                }
            }
        }
      });
    }
    

    Now your code that needs the document only runs after the document is actually available. This will work, but it does make the userExists function a bit less reusable. If you want to fix that, you can pass a callback into userExists that you then call after the document is loaded.

    public interface UserExistsCallback {
      void onCallback(boolean isExisting);
    }
    

    And use that in userExists as:

    public void userExists(String uid, final UserExistsCallback callback) {
      FirebaseFirestore db = FirebaseFirestore.getInstance();
      DocumentReference docRef = db.collection("users").document(uid);
      docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            boolean userExists = false;
            if (task.isSuccessful()) {
                DocumentSnapshot documentSnapshot = task.getResult();
                userExists = documentSnapshot.exists();
            }
            callback.onCallback(userExists);
        }
      });
    }
    

    And then invoke that from updateUI with:

    if (user != null) {
      userExists(user.getUid(), new UserExistsCallback() {
        public void onCallback(boolean isExisting) {
          if(isExisting){
            //Fill layout with the user data and the user linked document data
    
            //USER DATA
            txvNombre=findViewById(R.id.nombrePerfil);
            txvNombre.setText(user.getDisplayName());
            imvAvatar=findViewById(R.id.imvVistaPerfilAvatar);
            Picasso.with(VistaPerfilActivity.this)
                    .load(user.getPhotoUrl())
                    .resize(500,500)
                    .centerCrop()
                    .into(imvAvatar);
    
    
            //HERE GOES THE DOCUMENT DATA
    
    
          }else{
    
          }
        } else {
          finish();
        }
      });
    }
    

    As you can see our `` callback is quite similar to the OnCompleteListener of Firestore itself, it's just a bit more tailored to our needs.

    This problem pops up a lot, so I recommend spending some time learning more about it. See:

    • get all table values from firebase null object reference firebase database
    • Setting Singleton property value in Firebase Listener
    0 讨论(0)
提交回复
热议问题