0

Actually i'm storing map object under my firestore document with some ids of users which i block like this.click link below to view image

i have all members ids which i block in my social app and i need to get all those members document using firestore query but query doesnot work if arraylist has more than one index.but when i have only one id the query works perfect. here is the code

 private ArrayList<String> blocks =new ArrayList<>();
    if (logedInMember != null){
        blocks = logedInMember.getBlocks();
        if (!blocks.isEmpty()){
            Query query;
            CollectionReference collection = firestore.collection(Constants.MEMBERS);
            query  = collection;

            for (int i = 0 ; i< blocks.size(); i++){
                Log.d("block member ID :", blocks.get(i) );
                // now its time to query all these ids
                String id = blocks.get(i);
                if (!id.isEmpty()){
                    query = query.whereEqualTo(Constants.ID,id);
                }
            }

            query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()){


                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Member member = document.toObject(Member.class);
                            Log.d("Member Id :", member.getId());
                            Log.d("Member Name :", member.getName());

                        }
                    }else {
                       // loader.dismissProgress();
                        Log.d("error : ","fail to load query");
                    }
                }
            });
}
}
Yasir Ali
  • 155
  • 1
  • 11

1 Answers1

0

query doesnot work if arraylist has more than one index.but when i have only one id the query works perfect

try this

// RETRIEVING ALL Queries
Query allQueries = firebaseFirestore
                    .collection("yourcollection")                    
                    .orderBy("timestamp", Query.Direction.DESCENDING)
                    .limit(5);

            allQueries.addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {


                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            // get all ids
                            String postId = doc.getDocument().getId();
                            contentProfileDashboard = doc.getDocument().toObject(ContentProfileDashboard.class);                                                             
                                                     contentListDashboard.add(contentProfileDashboard);                         

                            // fire the event           
                            adapterProfileDashboard.notifyDataSetChanged();
                        }
                    }


                }
            });
Bill Gates
  • 215
  • 2
  • 11
  • this is the simple query . in my case the issue is for compound query OR multiple queries in single read operation – Yasir Ali Mar 21 '19 at 07:21
  • no issue in your code but you are performing simple query with no "where" clause. kindly read my question i have some ids of documents under my "members" collection and i want to fetch only those documents with ids which i have in my arraylist..thats why i'm performing a loop to append every id in query..but it doesnot work if arraylist of ids is grater than one index. – Yasir Ali Mar 21 '19 at 08:06
  • need time to understand your problem – Bill Gates Mar 22 '19 at 04:26
  • 1
    is this what you are looking for ? *https://stackoverflow.com/questions/46721517/google-firestore-how-to-get-document-by-multiple-ids-in-one-round-trip* @YasirAli – Bill Gates Mar 23 '19 at 16:54
  • yes exactly. but there is no perfect solution there that how to get multiple documents from collection by passing multiple ids to query. if u know any other data structure/model then share with me . thanks – Yasir Ali Mar 24 '19 at 12:47
  • currently i am storing some users object under "blocks" collection which is duplication of document in NOSQL case..as i didn't find any solution of my question thats why i go for duplication technique. – Yasir Ali Mar 24 '19 at 12:51