indexed query with FirestoreRecyclerAdapter

限于喜欢 提交于 2019-12-11 09:53:11

问题


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.


回答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.



来源:https://stackoverflow.com/questions/48756129/indexed-query-with-firestorerecycleradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!