iCe*_*iCe 6 kotlin firebase google-cloud-firestore
我目前正在使用新的 Firestore 作为后端/数据库对 Android 应用程序进行概念验证。我需要通过他们的 id 获取一堆文档(它们都在同一个集合中)
现在,我正在遍历 id 列表并一个一个地获取它们并将它们存储在一个列表中,该列表又更新了应用程序中的 RecycleView。这似乎是很多工作,而且性能不是很好。
从 Firestore 获取文档列表而不必循环所有 id 并一个一个地获取它们的正确方法是什么?
现在我的代码看起来像这样
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我使用的方式,直到他们添加此选项
我是用 AngularFire2 做的,但如果你使用的是 react 或 vue,那么 rxfire 库也是一样的。如果有一些文档被删除,您可以在订阅之前映射空项目。
const col = this.fire.db.collection('TestCol');
const ids = ['a1', 'a2', 'a3', 'a4'];
const queries = ids.map(el => col.doc(el).valueChanges());
const combo = combineLatest(...queries)
.subscribe(console.log)
Run Code Online (Sandbox Code Playgroud)
Firestore 目前不支持通过 ID 查询。
根据AngularFirebase,这在未来的开发路线图中,但它不是官方的:
请记住,Firestore 仍处于测试阶段。Firebase 工程师在路线图上暗示了一些非常酷的功能(地理查询、通过 id 数组查询)-我一定会及时通知您:)
| 归档时间: |
|
| 查看次数: |
5956 次 |
| 最近记录: |