0

I have a list of IDs of favorite items of a user locally stored in a list of strings. Now when user opens his favorite section the app needs to fetch all the items in the local favorite list from Cloud Firestore. Now what I see is only query that accepts a field and only a string (in my case only a single id of favorite item at a time).

Is there a way to pass the list of IDs as a Query in a single operation and get the result. What I am doing now is passing the IDs to RecylerView adapter and fetching it one by one in the BindViewHolder.

@Override
public void onBindViewHolder(final ImageViewHolder holder, int position) {

    db.whereEqualTo("imageId",mUploads.get(position))
            .get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            List<Upload> uploadsList = queryDocumentSnapshots.toObjects(Upload.class);
            Upload uploadCurrent = uploadsList.get(0);

            holder.materialCardView.setCardBackgroundColor(Color.parseColor(uploadCurrent.getImageColor()));
            Glide.with(mContext)
                    .load(uploadCurrent.getImageUrl())
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .centerCrop()
                    .into(holder.imageView);

        }
    });

}
2
  • i don't believe there is. I'm pretty sure you'd have to do a for loop and pass the data to your adapter and notify it or just continue doing what you're doing. Commented Jul 11, 2019 at 12:26
  • Repeating the data fetch every time in the adapter is a little overhead right? Commented Jul 11, 2019 at 12:30

1 Answer 1

2

Is there a way to pass the list of IDs as a Query in a single operation and get the result.

No, there is not not. You cannot pass a list or an array of strings to your query and get the results in one go. When calling get() on a Query object, the object that is returned is of type Task. Iterate through your list, create those Task objects and add all of them to a List<Task<DocumentSnapshot>> and then pass that list to Tasks's whenAllSuccess() method as it is explained in my answer from the following post:

Edit:

You should iterate through your list of strings and create the Task objects.

Task<DocumentSnapshot> documentSnapshotTask = collRef.document(stringFromList).get();

So at every iteration add the Task object to a list of tasks:

List<Task<DocumentSnapshot>> tasks = new ArrayList<>();

At the end just pass tasks list to whenAllSuccess() method, that's it.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry but I didn't quite understood that.The list of my IDs in List<String> and I need to get result list as of the form List<Uploads>. My collections name is "uploads" and the field of document that should be matched to is "imageId"
Check also the duplicate, it's the exact same thing, right?
But we should pass a field and its corresponding value as query in that case right?
Please see my updated answer and tell me if it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.