关于Firestore,是否有一些标记可以检查数据是否为联机/脱机数据? [英] About Firestore is there some flag I can check if the data is on/off line data?

查看:43
本文介绍了关于Firestore,是否有一些标记可以检查数据是否为联机/脱机数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读除非设备在线,否则 onComplete()方法将永远不会返回,以便可以将数据持久保存到Firestore的后台.

I read when on Android in offline mode the onComplete() method will never return unless the device goes online so data can be persisted to the backen at Firestore.

当我处于脱机模式并执行此 get()时,我注意到它返回了我认为必须是本地数据的内容,因为我有 setPersistenceEnabled(true)

When I'm in offline mode and do this get() I notice that it return with what I think must be local data since I have the setPersistenceEnabled(true)

  query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
      @Override
      public void onComplete(@NonNull Task<QuerySnapshot> task) {

       }
  });

我只想问一下这是否正确?如果有,我可以检查数据是否为在线/离线数据吗?

I just have to ask if this is correct? If so is there some flag I can check if the data is online/offline data?

有时这样做 get()并不会永远返回,只是永远挂在那里,这可能与我的设备在经过大量调试后变热有关.

Sometimes doing this get() it does not return just hangs there forever, maybe that has to do with my device getting warm after heavy debugging.

推荐答案

与Firebase Realtime数据库中要启用离线持久性的地方不同,您应该首先使用以下代码行:

Unlike in Firebase Realtime database where to enable offline persistence you should have used first, the following line of code:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

在Cloud Firestore中,对于Android和iOS,默认情况下启用离线持久性.因此,无需使用: setPersistenceEnabled(true).

In Cloud Firestore, for Android and iOS, offline persistence is enabled by default. So, there is no need to use: setPersistenceEnabled(true).

当您离线且正在使用 get()调用时,结果将来自您的应用程序正在使用的Cloud Firestore数据的缓存副本.

When you are offline and you are using a get() call, the result will be from the cached copy of the Cloud Firestore data that your app is actively using.

要检查数据是来自缓存还是来自Firestore服务器,您可以使用以下代码行:

To check if the data is from cache or from Firestore servers, you can use the following line of code:

String source = querySnapshot.getMetadata().isFromCache() ? "Local Cache" : "Firebase Server";

这篇关于关于Firestore,是否有一些标记可以检查数据是否为联机/脱机数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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