android - 从缓存读取的 Firestore 应用程序不起作用
问题描述
我已经设置了类似于示例快速启动应用程序的 android firestore 应用程序,换句话说:
EntryActivity
显示集合中的文档- 单击文档时,将文档 ID 传递给
DetailsActivity
DetailsActivity
使用它自己的FirebaseFirestore
参考来读取带有 ID 的文档- 两者都
EntryActivity
通过inDetailsActivity
获取自己的FirebaseFirestore
引用FirebaseFirestore.getInstance()
onCreate
但是,由于正在打开的文档以前是在 中读取的EntryActivity
,所以我希望DetailsActivity
从本地缓存中读取它比从服务器读取它更快。
在 中DetailsActivity
,从缓存中请求文档时:
var time = System.currentTimeMillis()
firestore.collection(collectionPath)
.document(docId)
.get(Source.CACHE)
.addOnSuccessListener { snapshot ->
time = System.currentTimeMillis() - time
Log.v(TAG, "read took $time ms fromCache=${snapshot.metadata.isFromCache}")
}
结果是:
read took 300-500 ms fromCache=true
从服务器读取:
var time = System.currentTimeMillis()
firestore.collection(collectionPath)
.document(docId)
.get(Source.SERVER)
.addOnSuccessListener { snapshot ->
time = System.currentTimeMillis() - time
Log.v(TAG, "read took $time ms fromCache=${snapshot.metadata.isFromCache}")
}
结果是:
read took 300-500 ms fromCache=false
这些运行多次以得出每个的平均时间。从缓存读取时速度没有明显差异。
EntryActivity
为了进行实验,我尝试使用上述相同的方法阅读文档(在已经阅读之后)get(Source.CACHE)
,并且速度更快:
read took 30-80 ms fromCache=true
为什么在第一个活动中从缓存中读取速度更快?
解决方案
推荐阅读
- android - 如何使用背景颜色不等于 colorPrimary
- python - 重用打开的连接到另一个脚本
- c# - 保存购买的物品统一
- javascript - React:如何将字符串发送到函数组件
- python - 时间序列预测中的负值
- javascript - 如何获得ajax请求的结果?
- angular - 无法从 asp.net core api 获取数据到角度 6
- kubernetes - 面向用户的服务的 Istio 请求路由不适用于 ingress-gateway
- python - 使用类计算年龄
- ios - 如何不使用 App Store 或 Enterprise 帐户在内部发布 IOS 应用程序?