在Firestore中按ID提取多个文档 [英] Fetch multiple documents by id in Firestore
问题描述
我目前正在使用新的Firestore作为后端/数据库对Android应用程序进行概念验证.我需要按其ID提取一堆文件(它们都在同一个集合中)
I'm currently doing a proof of concept for an Android app with the new Firestore as backend/db. I need to fetch a bunch of documents by their id (they are all in the same collection)
现在,我遍历id列表,并将它们逐个获取,并将它们存储在列表中,这反过来又会更新应用程序中的RecycleView.这似乎是一项艰巨的工作,并且效果不佳.
Right now, I'm looping thru the id list and fetching them one by one and storing them in a list which in turn updates a RecycleView in the app. This seems to be a lot of work and it does not perform very well.
从Firestore中获取文档列表而不必循环所有id并使其一一获取的正确方法是什么?
What is the correct way to fetch a list of documents from Firestore without having to loop all the ids and getting them one by one?
现在我的代码看起来像这样
Right now my code looks like this
for (id in ids) {
FirebaseFirestore.getInstance().collection("test_collection").whereEqualTo(FieldPath.documentId(), id)
.get()
.addOnCompleteListener {
if (it.isSuccessful) {
val res = it.result.map { it.toObject(Test::class.java) }.firstOrNull()
if (res != null) {
testList.add(res)
notifyDataSetChanged()
}
} else {
Log.w(TAG, "Error getting documents.", it.exception)
}
}
}
推荐答案
Firestore当前不支持按ID查询.
Firestore does not currently support query by IDs.
根据 AngularFirebase ,这是未来的发展路线,但不是官方的:
According to AngularFirebase, this is in the roadmap for development in the future, but its not official:
请记住,Firestore仍处于测试阶段. Firebase工程师在路线图上暗示了一些非常酷的功能(地理位置查询,按ID数组查询)-我一定会向您保证:)
Keep in mind, Firestore is still in beta. Firebase engineers hinted at some really cool features on the roadmap (geo queries, query by array of ids) - I’ll be sure to keep you posted :)
这篇关于在Firestore中按ID提取多个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!