问题描述
FirebaseUI-Android
在使用FirebaseRecyclerAdapter
时提供索引查询.如此处所述,当访问Firebase Realtime DB
数据时,该方法效果很好: https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-with-indexed-data
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
但是访问Firestore
数据呢?似乎FirestoreRecyclerAdapter
不支持通过setIndexedQuery(keyref, dataref, ...
进行索引查询))如何在RecyclerView
内部手动执行数据查询?
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
?
更新:一些人声称Firestore不再需要索引查询,这是因为内置索引功能使方案设计比Firebase Realtime DB容易得多.好吧,我不同意.
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本来可以让我检索交易列表(键)以及与每个交易(数据)相对应的项目记录. Firestore索引虽然功能强大,但在这种情况下无济于事.
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.
推荐答案
在FirestoreRecyclerOptions
类下没有setIndexedQuery()
方法,因为不需要这种方法.从有关索引的官方文档,
There is no setIndexedQuery()
method beneath FirestoreRecyclerOptions
class because there is no need for a such a method. From the offical documentation regarding indexes,
如果您需要使用多个属性的查询,则需要从Firebase控制台手动创建一个新索引. Firestore尚不支持以编程方式创建索引.
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.
是的,在收藏下面没有收藏,在文档下面没有文档.但是您可以使用以下结构:
That's correct, there are no collections beneath collections or documents beneath documents. But you can use the follwing structure:
Collection -> Document -> Collection
但是请记住,使用嵌套的集合和文档现在在Firestore中是一种常见的做法.假设您在items
下面有transactions
.在Firebase实时数据库中,如果您有一个嵌套在另一个对象中的对象,则每当您要查询数据库以仅显示项目时,就会将Items
对象与Transactions
对象一起下载,结束花更多的带宽.但是当涉及到收藏和文档时,情况就不再如此.
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.
我不了解您的应用程序的用例,因为那里是因为信息较少,但Cloud Firestore在创建集合和文档之间的关系时要容易得多.如果有时间,可以看看我的有关如何创建Firestore数据库结构的教程.
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.
即使您使用的是Firebase Realtime数据库或Cloud Firestore数据库,反规范化也是正常的.因此,不要害怕重复数据.
Even if you are using Firebase Realtime database or Cloud Firestore database, denormalization is normal. So don't be afraid to duplicate data.
这篇关于使用FirestoreRecyclerAdapter进行索引查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!