1

I'm trying to get different subcollections and adapt them to my RecyclerAdapter with Firebase-UI.

My Cloud Firestore is set like this:

user:(Collection)
    uid1:(Document)
       name, email(fields)

data:(Collection)
    uid1:(Document)
       images:(Collection)
          document1
                   fields
       pdf:(Collection)
          document1
                  fields

So I was thinking and I don't know how to query different Collections with the same model and adapt them into the recycler adapter.

My actual code can retrieve images:

private void setupImages(){
    Query query = fStore.collection("data").document(userID).collection("images"); //get images collection

    FirestoreRecyclerOptions<Data> options = new FirestoreRecyclerOptions.Builder<Data>().setQuery(query,Data.class).build(); 

    FirestoreRecyclerAdapter adapter = new ImageAdapter(options, getContext());

    recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

    recyclerView.setAdapter(adapter); //adapt the query to adapter
}

And the class binds the data to the view with the Data model.

I've thought about creating a list and adding the items, but I don't think it's a good way and there should be other options.

Question: How can I bind different collections into the recycler adapter without being replaced?

PS: Feel free to comment in any way to improve my database. I'm new!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Barrufet
  • 495
  • 1
  • 11
  • 33

1 Answers1

1

According to the answer:

Yes, that's it!

For this question:

So you want to get the data from data/userID/images and data/userID/pdfs in a single query?

The answer is, you cannot. There is no way you can get data from two different collections in a single Query, even if the objects within both collections are the same. The queries in Firestore are shallow, meaning that you can only get documents from the collection that the query is run against. If you need to get the documents from both collections, you need to create two separate queries. In Android, you can achieve this, with the help of Tasks whenAllSuccess() method, as explained in my answer from the following post:

However, there is a solution to get data from multiple collections but all collections/sub-collections should ave the same name. For that, please see the official documentation regarding the use of Firestore collection group queries.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • damn, that's unlucky since getting a single query by every collection and adding it to the recycler adapter it just replaces it, should I change the database structure and put all documents from image/pdf collections in a single collection and then get them with a query? – Barrufet Apr 12 '20 at 14:45
  • 1
    Yes, that's a common practice and you should go ahead with it. Remember to create a property named `type` and store there `image` or `pdf`. To get all `image` objects simply use a `wehereEqualTo("type", "image")` call. – Alex Mamo Apr 12 '20 at 14:49
  • 1
    Thank you very much for helping me, I appreciate your patience! – Barrufet Apr 12 '20 at 14:53