如何强制在Firestore中获取缓存的文档?我想在联机时获取缓存的文档 [英] how to force to get cached documents in Firestore ? I want to get cached documents when online

查看:60
本文介绍了如何强制在Firestore中获取缓存的文档?我想在联机时获取缓存的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这种方法可以从Firestore查询文档:

so I have this method to query documents from Firestore:

FirestoreCollectionReference.event.getReference()
            .whereEqualTo(FIRESTORE_EVENT_FIELD_CITY,selectedCity)
            .whereEqualTo(FIRESTORE_EVENT_FIELD_HAS_BEEN_APPROVED,true)
            .whereGreaterThanOrEqualTo(FIRESTORE_EVENT_FIELD_DATE_START_TIME,now.toDate())
            .whereLessThanOrEqualTo(FIRESTORE_EVENT_FIELD_DATE_START_TIME,oneMonthFromNow.toDate())
            .orderBy(FIRESTORE_EVENT_FIELD_DATE_START_TIME, Query.Direction.ASCENDING)
            .orderBy(FIRESTORE_EVENT_FIELD_RANKPOINT, Query.Direction.DESCENDING)
            .limit(limit)
            .get()
            .addOnSuccessListener { snapshot ->

                val eventDocuments = snapshot.documents
                val eventArray = ArrayList<Event>()

                for (document in eventDocuments) {
                    val eventData = document.data
                    val event = Event(dataEvent = eventData)
                    eventArray.add(event)
                }

                completion(null,eventArray)

            }.addOnFailureListener {
                completion(it.localizedMessage,null)
            }

我了解,如果我的应用程序使用该方法脱机,firestore将自动使用缓存的文档.

I understand that firestore will automatically using the cached documents if my app is offline using that method.

但是我想要的是....

but what I want is ....

让我的应用程序始终在线.但我不希望每次打开应用程序时,它始终会从服务器中获取数据.我想限制一下.

let say my app will ALWAYS be online. but I don't want everytime I open the app, it will always fetching data from server. I want to give limitation.

如果最后一个时间戳用户从服务器获取数据的时间超过一个小时前,那么可以,从服务器获取数据并显示加载指示符.否则,仅从先前从服务器获取的缓存文档中获取数据,并且不显示加载指示符.

if the last timestamp user fetching data from server is more than one hour ago, then ok, fetching data from server and show up the loading indicator. otherwise just fetch data from cached documents that previously fetched from server and don't show loading indicator.

我可以这样做吗?如何在Firestore中强制获取缓存的文档?即使用户在线,我也想获取缓存的文档

can I do that ? how to force to get cached documents in Firestore ?I want to get cached documents even when the user is online

我已尝试从此处 https:/阅读文档/firebase.google.com/docs/firestore/manage-data/enable-offline 但是似乎只有在发生脱机情况下才能检索缓存的文档.该示例适用于实时侦听器.就我而言,我只使用 get()

I have tried to read the documentation from here, https://firebase.google.com/docs/firestore/manage-data/enable-offline but it seems the cached documents can only be retrieved only if the offline case happened. and the example is for realtime listener. in my case, I only use get()

推荐答案

如果要显式控制 get()从何处获取数据,可以传入 Source 参数,告诉它该怎么做:

If you want to explicitly control where get() gets its data from, you can pass in a Source parameter that tells it what to do:

.get(Source.CACHE)

.get(Source.SERVER)

另请参见指南 get(Source)的参考文档.

Also see the guide and reference documentation for get(Source).

这篇关于如何强制在Firestore中获取缓存的文档?我想在联机时获取缓存的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆