首页 > 解决方案 > 从缓存读取的 Firestore 应用程序不起作用

问题描述

我已经设置了类似于示例快速启动应用程序的 android firestore 应用程序,换句话说:

但是,由于正在打开的文档以前是在 中读取的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

为什么在第一个活动中从缓存中读取速度更快?

标签: androidfirebasegoogle-cloud-firestore

解决方案


推荐阅读