本文介绍了无法在Firestore中禁用离线数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的Firestore Database中删除数据后,我的Android app需要一些时间才能意识到该数据已被删除,并且我认为它是由于自动数据缓存而发生的.我的应用与离线使用无关,我想禁用此功能...

After deleting data from my Firestore Database, it takes my Android app some time to realize that the data was deleted, and I assume that it's happening due the auto data cache. My app has nothing to do with offline usage and I'd like to disable this feature...

我已在我的自定义Application Class中添加了此内容:

I have added this in my custom Application Class:

import android.app.Application;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreSettings;

public class ApplicationClass extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseFirestore db=FirebaseFirestore.getInstance();
        FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
                .setPersistenceEnabled(false)
                .build();
        db.setFirestoreSettings(settings);
    }
}

关闭Internet连接并重新打开(在应用程序仍在运行时,无论是否在后台运行)后,都会出现问题-Firestore module似乎失去了与Internet的连接服务器,并且它执行与预期操作相反的操作-而不是 stop 从缓存中获取数据,而是仅从缓存中 获取数据.

The problem occurs after turning off the internet connection and than turning it back on (while the app is still running, in the background or not)- the Firestore module seems to lose connection to the server, and it makes the opposite operation than the intended one - instead of stop taking data from the cache, it takes data from the cache only.

例如,调试此代码将始终显示 isFromCachetrue documentSnapshot为空(即使在server端也是如此) -它不为空):

For example, debugging this code will always show that isFromCache is true and documentSnapshot is empty (even though that on the server side - it's not empty):

usersRef.document(loggedEmail).collection("challenges_received").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot documentSnapshots) {
        boolean isFromCache=documentSnapshots.getMetadata().isFromCache();
        if (!documentSnapshots.isEmpty()) {
        }
    }
});

这是正常行为吗?

还有另一种方法来禁用Cloud Firestore中的数据缓存吗?

Is there another way to disable the data cache in Cloud Firestore?

添加:自定义Application Class中的FirebaseFirestore.setLoggingEnabled(flase);(而不是上面的代码)给出相同的结果.

Adding: FirebaseFirestore.setLoggingEnabled(flase); (instead of the code above) in the custom Application Class gives the same result.

推荐答案

根据Cloud Firestore 16.0.0 SDK更新,现在有此问题的解决方案:

According to Cloud Firestore 16.0.0 SDK update, there is now a solution to this problem:

现在,您可以选择是否要仅从服务器缓存获取数据,例如此(仅用于服务器的示例):

You are now able to choose if you would like to fetch your data from the server only, or from the cache only, like this (an example for server only):

DocumentReference documentReference= FirebaseFirestore.getInstance().document("example");
documentReference.get(Source.SERVER).addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
            //...
    }
});

仅对于缓存,只需将上面的代码更改为Source.CACHE.

For cache only, just change the code above to Source.CACHE.

默认情况下,这两种方法仍会尝试使用服务器并回退到缓存.

这篇关于无法在Firestore中禁用离线数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:11