Reputation: 2607
Need to get list of objects based on LIST of parameters not only 1 parameter.
Step 1: we get list of uidUsers from one collection ONE and save it as a list Step 2: in collection TWO I need to get only those documents(objects with values) where document is equal value from the list from step 1.
I wanted to filter documents by compare document name with uid in the list. But didn't find any solutions in API. And now maybe it is possible to save uidUser in the field of document so I could try to find by comparing key-value of document field with value from list. But i dont know how.
There is a small code i have
public void getNeededUsers(List<String> uidList, UsersListCallback usersListCallback){
Query query = db.collection(Consts.COLLECTION_USERS);
for (String uid: uidList) {
query.whereEqualTo("uid", uid);
}
query.get().addOnCompleteListener(task -> {
List<User> userList = new ArrayList<>();
for(DocumentSnapshot snapshot: task.getResult().getDocuments()){
userList.add(snapshot.toObject(User.class));
}
usersListCallback.getUsers(userList);
});
}
Upvotes: 1
Views: 2493
Reputation: 3001
Try this:-
public void getNeededUsers(List<String> uidList, UsersListCallback usersListCallback){
List<User> userList = new ArrayList<>();
for (int i = 0; i < uidList.size(); i++){
db.collection("Collection A").whereEqualTo("uid", uidList.get(i))
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
userList.add(documentReference.getId());
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(ActivityName.this, "Error In Fetching Uid's" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
Now userList will contain all the matched uid's. Now retrieve data from collection 2 based on these uid's.
By calling fetchData(userList);
public void fetchData(List<String> documentList)
{
for (int j = 0; j < documentList.size(); j++){
db.collection("Collection B").whereEqualTo("uid", documentList.get(i))
.get()
.addOnCompleteListener(this, new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(Task<QuerySnapshot> task) {
if (task.isSuccessful())
{
for (QueryDocumentSnapshot documentSnapshot : task.getResult())
{
//Here you can fetch data or convert it to object
}
}
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(ActivityName.this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
}
}
Upvotes: 2