我已经设置了类似于示例快速启动应用程序的 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
为什么在第一个活动中从缓存中读取速度更快?