首页 文章

除非禁用脱机持久性,否则Firestore会对大型集合进行慢查询

提问于
浏览
3

我正在使用Firestore Recycler Adapter绑定到firestore中的大型集合的查询 .

据我了解GitHub中的文档,Firestore Recycler Adapter使用.addSnapshotListener(),而不是.get()方法 .

该系列有5,000个文件,其中我将限制为100个 .

Query query = fsDB.collection("Users").document(user_id).collection("posts")
.orderBy("date_created).orderBy("topic").limit(100);

现在,当我连接到Internet并且启用了脱机持久性时,此查询在调用onDataChanged()之前平均需要90秒 . 我认为这是因为回收者从设备上的内存或缓存中获取数据,然后在从网络获取数据之前对其进行排序(!) .

因为,当禁用脱机持久性时,查询只需要2-3秒 .

如何在启用脱机持久性的同时更快地使用此查询?或者是否有一种方法可以使侦听器在搜索内存/缓存之前访问网络索引?

就像是...

FirestoreRecyclerOptions<Reed> options = new FirestoreRecyclerOptions.Builder<>()
.setQuery(query, Reed.class)
.setFirst(FirebaseFirestore.getInstance fsDB);

客户端的firestore索引不可用吗?

使用firestore 15.0.0,Android设备

2 回答

  • 0

    离线查询未像应用程序在线时那样进行优化 . 快速查询是Firestore后端的一项功能,在离线时显然不可用 .

    另见:How reliable is Firestore as an offline persistence mechanism?

  • 5

    @Doug Stevenson's answer(加上评论)了解原因,但至于解决方案,我建议采用混合方法 . 使用所需的索引以及可能的自己的缓存 - 清除/缓存限制方案来维护您自己的本地数据库或ORM缓存 .

    使用Firestore 's built-in persistence for the sync capabilities, and add a Snapshot listener to trigger a refresh of your offline cache. You' ve有一些优化选项,从checking only for the delta到使用 snapshot.getMetadata().hasPendingWrites() 只有在线/新数据可用时才会触发 .

    当然,您仍然可以在UI中实现按需刷新行为 .

    这种方法应该为您提供所需的(自我调整的)离线性能,同时保留Firestore后端的优势 .

相关问题