1

FirebaseUI-Android provides for indexed queries when using a FirebaseRecyclerAdapter. That works well when accessing Firebase Realtime DB data as explained here: https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-with-indexed-data

But what about accessing Firestore data? It seems that FirestoreRecyclerAdapter does not support indexed queries via setIndexedQuery(keyref, dataref, ...) How can I maybe execute the data query manually inside the RecyclerView?

UPDATE: several people have claimed that Firestore doesn't need indexed queries anymore due to the build-in indexing with makes the schema design much easier than it was with Firebase Realtime DB. Well, I disagree.

Lets say I have a bunch of related items and transactions which I want to query by item and by transaction.

I can't make the transactions a subcollection of items as this would make it impossible to retrieve a list of all transactions. I also can't replicate the item data as redundant fields in each transaction since the data is mutable.

Given these constrains I am stuck with making items and transactions two independent collections which need to be combined again inside the app:

items: {
        "item1": {
                  "name": "some editable label",
                  [more fields]
                 }

transactions: {
               "trans1": {
                          "itemid": "item1"
                          [more fields]
                         }
               "trans2": {
                          "itemid": "item1"
                          [more fields]
                         }
              }

FirebaseRecyclerAdapter.setIndexedQuery would have allowed me to retrieve a list of transactions (key) and the corresponding item record with each transaction (data). Firestore indexing, while powerful, does not help in this situation.

duffy
  • 615
  • 1
  • 9
  • 25
  • I have always asume there is none, because Firestore promise is to always response query results as fast as posible, and with Firestore queries can have multiple parameters, so there is no reason to create an index – cutiko Feb 13 '18 at 01:05

1 Answers1

1

There is no setIndexedQuery() method beneath FirestoreRecyclerOptions class because there is no need for a such a method. From the offical documentation regarding indexes,

Cloud Firestore requires an index for every query, to ensure the best performance. All document fields are automatically indexed, so queries that only use equality clauses don't need additional indexes.

If you need a query that is using more than one propoerty, you need to manually create a new index from the Firebase console. To create an index programmatically is not supported yet by Firestore.

Edit:

I can't make the transactions a subcollection of items as this would make it impossible to retrieve a list of all transactions.

That's correct, there are no collections beneath collections or documents beneath documents. But you can use the follwing structure:

Collection -> Document -> Collection

But remember, using nested collections and documents it's now a common practice in Firestore. Let asume you have transactions beneath items. In the Firebase Realtime Database, if you had a object nested within another object, every time you would have wanted to query your database to display only the items, the enitire Items object would have downloaded together with the Transactions object, ending to spend more bandwith. But when it comes to Collections and Documents that's not the case anymore.

I don't understand the use case of your app because there because there is less information but there is much easier in Cloud Firestore to create relations between collections and documents. If you have time, you can take a look on one of my tutorials on how to create a Firestore database structure.

Even if you are using Firebase Realtime database or Cloud Firestore database, denormalization is normal. So don't be afraid to duplicate data.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • The issue is that Firestore does not allow to query two collections. The indexed query of the firebasedb recycler was a workaround. I've edited my question to include a use case. – duffy Feb 13 '18 at 10:27