0

I'm currently creating an Android application that works with Firestore, and I would like to implement a method / class to check if there is a document with a given path in one of the collections.

I tried to do this by trying to get data from one document, then if the object does not exist, I wanted to return the value of the second path, and the default would be the value of the path checked. And here's the problem, since it's an anonymous class, I can't assign anything to a variable outside of that class. Below is the code and a preview screen from my Firebase database.

public class UserValid
{
    String typeOfAcc = "Podopieczni";
    FirebaseFirestore fStore;
    FirebaseAuth fAuth;
    DocumentReference documentReference;
    List itemList = new ArrayList<>();
    String userID;

    public UserValid()
    {
        Log.d("TAG", "Błąd, wybrano konstruktor bez argumentów");
    }

    //Metoda sprawdzająca jaki typ konta jest aktualnie obsługiwany
    // Jeśli wartość true - konto podopiecznego, wartość false - konto trenera;
    public UserValid(FirebaseAuth fAuth, FirebaseFirestore fStore)
    {
        userID = fAuth.getCurrentUser().getUid();
        this.fAuth = fAuth;
        this.fStore = fStore;
        documentReference = fStore.collection("Podopieczni").document(userID);

        readData(new FirestoreCallback()
        {
            @Override
            public void onCallback(List<String> list)
            {
                if(itemList.get(0) == null)
                {
                    typeOfAcc="Trenerzy";
                    Log.d("TAG", itemList.toString());

                }
            }
        });


    }
    private void readData(FirestoreCallback firestoreCallback)
    {
        //DocumentReference docRef = db.collection("cities").document("SF");
        documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful())
                {

                    //for(DocumentSnapshot document : task.getResult()) {
                        String itemName = task.getResult().getString("Imie");
                        itemList.add(itemName);

                   // }
                    firestoreCallback.onCallback(itemList);
                }
            }

        });
    }

   
   
    private interface FirestoreCallback
    {
        void onCallback(List<String> list);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kunegundek
  • 21
  • 4
  • Firebase API is asynchronous. There is no way you can return `typeOfAcc` as a result of a method. So please check the duplicate to see how can you solve this using a custom callback. – Alex Mamo Jan 04 '21 at 10:52
  • Thank you! I'll try to solve it and let you know if it worked. :) – Kunegundek Jan 04 '21 at 11:14
  • Ok, give it a try and tell me if it works. – Alex Mamo Jan 04 '21 at 11:24
  • Okey, so I tried to fix in your way, but after this, when I display arraylist in log, is okey, but now, I want to return modify string (var typeOfAcc) to the another class, which this string will be correct document path.Of course, when I try to call a method that takes a String "typeOfAcc" after calling the check method, it gets the string before modifying it. What should I do to make a String to another class just after modification, not before? – Kunegundek Jan 04 '21 at 13:07
  • I edit code in my post ;) – Kunegundek Jan 04 '21 at 13:11

0 Answers0