0

I am trying to check where the document exists in the collection or not. but when I make query it returns false but as you see in photo document is exists, so where I am doing wrong?

 public MutableLiveData<Boolean> isConversationNodeExsits(String documentID) {

        if (isConversationNodeExsits == null ) {
            isConversationNodeExsits = new MutableLiveData<>();
             db.collection(Constants.CONVERCTION_ID)
                    .document("13005065233@umt,edu,pk---sifusir@fidelium10,com")
                     .get()
                    .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {

                            if (task.getResult().exists()) {
                                Log.d(TAG, " conversation node exists ");
                                isConversationNodeExsits.setValue(true);
                            } else {
                                Log.d(TAG, " conversation node not exists " + task.getResult().getReference().getPath());
                                isConversationNodeExsits.setValue(false);
                            }
                        }
                    });

        }

        return isConversationNodeExsits;
    }

enter image description here

Hamza
  • 2,017
  • 1
  • 19
  • 40
  • 3
    get() is asynchronous and returns immediately. The callback you pass to it is executed some time later when the query completes. You need to use the result of the query only inside the callback. – Doug Stevenson Aug 09 '18 at 14:56
  • can you please point to any example or doc – Hamza Aug 09 '18 at 14:58
  • thank you @Doug Stevenson I understand, my mind also like callback which takes some time to understand new things – Hamza Aug 09 '18 at 15:04
  • Please check the duplicates to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Aug 09 '18 at 15:12
  • i still face problem , it keep return false , but when i go one step deep then exists() return true , – Hamza Aug 09 '18 at 16:34
  • the problem was i try to check root document(not root collection) which contains collection then this collection contains document with unique ID then these uniqueId contains final documents message , so my tree look like /Cid/13005065233@umt,edu,pk---sifusir@fidelium10,com/0/MVmTs4P6qsiGZclK8nmp, but i was try to attach Listener at Cid/13005065233@umt,edu,pk---sifusir@fidelium10,com,thats why it returning false – Hamza Aug 09 '18 at 16:54

0 Answers0