如何确保Firebase只从服务器获取数据

huangapple go评论71阅读模式
英文:

How to make sure Firebase fetches data from the server only

问题

我想要能够只从Firebase服务器获取数据,如果没有互联网连接,它应该显示一个失败消息。

Source source = Source.SERVER;
firebaseFirestore.collection("stock").document(pid).get(source).addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                String prod_id = document.getString("pid");
            }
        }
    }
});

我尝试了上述方法,但如果没有互联网连接,它仍然加载缓存的结果。请帮助。

英文:

I want to be able to fetch data from Firebase server only and if there is no internet connection it should show a failure message.

  Source source = Source.SERVER;
  firebaseFirestore.collection(&quot;stock&quot;).document(pid).get(source).addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
        @Override
        public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
              if (task.isSuccessful()) {
                     DocumentSnapshot document = task.getResult();
                            if (document.exists()) {
                                String prod_id = document.getString(&quot;pid&quot;);
                       }
                                
                    }
                });

I tried doing the above but it still loads cached results if there is no internet.
<br>Please help.

答案1

得分: 0

Sure, here's the translated content:

这正是您的代码所做的。通过指定 `Source.SERVER`,您正在告诉Firestore只从服务器返回数据。

当没有网络连接时,`onFailure()` 只在读取文档时出现问题时触发,例如,当您无权访问它们时。换句话说,当Firebase服务器因设置在安全规则中的权限而拒绝读取操作时,`onFailure()` 永远不会触发。当用户设备没有网络连接时,它不会触发 `onFailure()`,这是有道理的,因为您需要在离线时访问数据。这 **不能** 被视为失败。

我尝试了上述操作,但如果没有网络,它仍然会加载缓存的结果。

这是正常行为。Firestore SDK会缓存应用程序正在积极使用的Cloud Firestore数据的副本,因此您的应用程序可以在设备离线时访问数据。

**编辑:**

根据 @FrankvanPuffelen 的评论,在[官方文档](https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/Source#public-static-final-source-server)中还提到:

&gt; **public static final Source SERVER**

&gt; 导致Cloud Firestore避免使用缓存,**如果无法访问服务器,则生成错误**。请注意,如果服务器请求成功,缓存仍将被更新。还要注意,延迟补偿仍然会生效,因此任何待处理的写操作将显示在返回的数据中(与服务器提供的数据合并)。
英文:

> I want to be able to fetch data from firebase server only

This is exactly what your code does. By specifying Source.SERVER, you are telling Firestore to return only data from the server.

> if there is no internet connection it should show a failure message.

onFailure() fires only when there is a problem in reading documents, for example, when you don't have permission to access them. In other words, when Firebase server rejects the read operation due to the permissions that are set the in security rules. onFailure() will never fire when there is no internet connection on user device and it makes sense since you need to access the data while offline. This cannot be considered a failure.

> I tried doing the above but it still loads cached results if there is no internet.

That's normal behavior. The Firestore SDK caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline.

Edit:

According to @FrankvanPuffelen comment, in the official documentation is also mentioned that:

> public static final Source SERVER

> Causes Cloud Firestore to avoid the cache, generating an error if the server cannot be reached. Note that the cache will still be updated if the server request succeeds. Also note that latency-compensation still takes effect, so any pending write operations will be visible in the returned data (merged into the server-provided data).

huangapple
  • 本文由 发表于 2020年8月13日 19:20:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63394057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定