1
    List<String> uniNames = new ArrayList<>();

    FirebaseFirestore db = FirebaseFirestore.getInstance();

    CollectionReference unisRef = db.collection("Universities");

    unisRef.get().addOnCompleteListener(task -> {
        if(task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                uniNames.add(document.getString("name"));
            }
        } else {
            Log.d("MainActivity", "Error getting documents: ", 
            task.getException());
        }
    });

I am trying to get all the values of the field name from my documents in the university collection but the .get() just doesn't seem to execute at all. it connects to the database just fine and i get a collectionreference object but the .get() just gets skipped completely so i end up with an empty List. Am i using the oncomplete listener wrong or missing something trivial?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
dontblink
  • 41
  • 3
  • An educated guess is that you're not used to asynchronous APIs. Put a breakpoint on `if(task.isSuccessful()) {` and run the code in a debugger. If the breakpoint does get hit, have a read on https://stackoverflow.com/questions/52057313/problems-with-firestore-connections-executing-threads/52059411#52059411 – Frank van Puffelen Nov 27 '18 at 14:50
  • i tried that but it never hits that breakpoint – dontblink Nov 27 '18 at 14:54
  • Hmmm.... I don't know why that would happen. The only thing I can think of is if your device can't make a connection to Cloud Firestore. In that case the client can't determine the result, so it won't call the completion handler. – Frank van Puffelen Nov 27 '18 at 15:02
  • In which part of your code are you checking that your list is empty? So you basically say that `document.getString("name")` is never triggered, right? Please responde with @. – Alex Mamo Nov 27 '18 at 15:05
  • @FrankvanPuffelen well it shows that a device connected in the firestore map thing which meant it dame a connection right? – dontblink Nov 27 '18 at 15:10
  • @AlexMamo yeah that whole get seems to never get triggered since i dont get the task exception or anything either, i just checked it in the debugger if the list is empty or not – dontblink Nov 27 '18 at 15:12
  • @dontblink Try to use this: `Log.d(TAG, document.getString("name"))` inside the for loop. Do you have any output in the logcat? – Alex Mamo Nov 27 '18 at 15:16
  • @AlexMamo ok i do see the right names i want to see so it does execute i guess, made a mistake somewhere else than. thanks for the help <3 – dontblink Nov 27 '18 at 15:24
  • @dontblink In this case, you should follow Frank's advice. I'll mark this question as a duplicate because it already has an answer.. – Alex Mamo Nov 27 '18 at 15:25
  • @FrankvanPuffelen Right guess puf! – Alex Mamo Nov 27 '18 at 15:26

1 Answers1

0

Check the error you are getting from your Log

If it's a PERMISSION_DENIED error check your rules from firebase console.

If rules are okay, make sure you are referencing the collection with the right string. And remember firebase collection referencing is case sensitive.

Anga
  • 2,450
  • 2
  • 24
  • 30