简体   繁体   中英

Can we make several requests with Firestore in android

On android with firestore I would like to do two conditions to display the result

here is my functional request

 Query query=firebaseFirestore.collection("users").whereEqualTo("chauffeurs","");

and I would like another equal condition ("driver", "Cyril") I have to try to give a.whereEqualTo ("drivers", "Cyril") but it does not return anything to me

我的火炉

I don't know if that's you're looking for but you can create an interface and you can get & use your data with a method. For example;

private interface Callback {
    void onData(List<Users> list1, List<Users> list2, List<Users> list3);
}

Interface contains 3 lists. First list contains documents where chauffeurs field equals Cyril, second list contains chauffeurs field equals Alexandre and the last one contains chauffeurs equals "" (you can change the interface as you want).

By the way Users is the Object that has a same fields like your documents inside users collection.

public class Users {

    private String adresse, adresse2, chauffeurs, codepostal;

    public Users() {
    }

    public Users(String adresse, String adresse2, String chauffeurs, String codepostal) {
        this.adresse = adresse;
        this.adresse2 = adresse2;
        this.chauffeurs = chauffeurs;
        this.codepostal = codepostal;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getAdresse2() {
        return adresse2;
    }

    public void setAdresse2(String adresse2) {
        this.adresse2 = adresse2;
    }

    public String getChauffeurs() {
        return chauffeurs;
    }

    public void setChauffeurs(String chauffeurs) {
        this.chauffeurs = chauffeurs;
    }

    public String getCodepostal() {
        return codepostal;
    }

    public void setCodepostal(String codepostal) {
        this.codepostal = codepostal;
    }
}

Method which you can access and use those lists/datas (it's just for example, you can change the logic as you want. It's up to you):

private void getData(Callback callback) {
        Task<QuerySnapshot> task1 = ref.whereEqualTo("chauffeurs", "Cyril").get();
        Task<QuerySnapshot> task2 = ref.whereEqualTo("chauffeurs", "Alexandre").get();
        Task<QuerySnapshot> task3 = ref.whereEqualTo("chauffeurs", "").get();

        Task<List<QuerySnapshot>> allTasks = Tasks.whenAllSuccess(task1, task2, task3);
        allTasks.addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                QuerySnapshot query1 = (QuerySnapshot) task1.getResult();
                QuerySnapshot query2 = (QuerySnapshot) task2.getResult();
                QuerySnapshot query3 = (QuerySnapshot) task3.getResult();

                List<Users> list1 = new ArrayList<>();
                List<Users> list2 = new ArrayList<>();
                List<Users> list3 = new ArrayList<>();

                for (DocumentSnapshot document : query1) {
                    if (document.exists()) {
                        list1.add(document.toObject(Users.class));
                    }
                }
                for (DocumentSnapshot document : query2) {
                    if (document.exists()) {
                        list2.add(document.toObject(Users.class));
                    }
                }
                for (DocumentSnapshot document : query3) {
                    if (document.exists()) {
                        list3.add(document.toObject(Users.class));
                    }
                }
                callback.onData(list1, list2, list3);
            }
        });
    }

And whenever you want to use/get those datas simply call that method:

getData((list1, list2, list3) -> {
   //Documents where chauffeurs field equals Cyril
   for(int i = 0; i < list1.size(); i++){
       Users user = list1.get(i);
       String adresse = user.getAdresse();
       String adresse2 = user.getAdresse2();
       String chauffeurs = user.getChauffeurs();
       String codepostal = user.getCodepostal();
   }
   //Documents where chauffeurs field equals Alexandre
   for(int i = 0; i < list2.size(); i++){
       Users user = list2.get(i);
       String adresse = user.getAdresse();
       String adresse2 = user.getAdresse2();
       String chauffeurs = user.getChauffeurs();
       String codepostal = user.getCodepostal();
   }
   //Documents where chauffeurs field equals ""
   for(int i = 0; i < list3.size(); i++){
       Users user = list3.get(i);
       String adresse = user.getAdresse();
       String adresse2 = user.getAdresse2();
       String chauffeurs = user.getChauffeurs();
       String codepostal = user.getCodepostal();
   }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM