4

I have a collection called 'posts' and I have 11 documents in it. This is my database: enter image description here

Each document contains an image. So , i have created an activity which displays the images in each document in a recyclerview. And when I click on any image, it send the postId to a second activity which shows the rest of the fields of that particular document in the second activity. I use the following code to retrieve document fields in the second activity:

DocumentRefernce docRef = FirebaseFirestore.getInstance()
        .collection("posts")
        .document(postId)
        .get()
        .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>()
        {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot)
            {
                System.out.println("DC Exists: " + documentSnapshot.exists());
                System.out.println("DC ID: " + documentSnapshot.getId());
            }
        });

All the images are displayed fine in the first activity and the postId is send successfully to the second activity. But the problem is when I run the above code,for some documents, documentSnapshot.exists() returns false, but I am able to retrieve the id with documentSnapshot.getId() properly for all the documents. Also, all documents have same fields. Is there anything wrong with the code?

Edit: I have tried this with the OnCompleteListener, and still some documets return false when called exits(). It is not throwing any exceptions.

Jayesh Babu
  • 1,389
  • 2
  • 20
  • 34
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/197071/discussion-on-question-by-jayesh-babu-why-some-documentsnapshot-exists-returns). – Samuel Liew Jul 27 '19 at 01:17
  • Have you tried to hardcode the `postId` and see what is happens? – Alex Mamo Jul 27 '19 at 10:14
  • I solved it. If u see this document https://drive.google.com/file/d/1JqcXxh6p3BU7odJKeRJKfMkyYzzBORQ1/view , in the `postId` field, there is an extra space at the end of the postId value. I think that was causing the problem. I couldn't figure that out because when i run the above code, even though the `exists()` returned false, the `getId()` method returned the valid document Id. Anyway, thanks for ur help. – Jayesh Babu Jul 27 '19 at 11:16

0 Answers0