问题描述
从我的 Firestore 数据库
删除数据后,我的 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);
}
}
问题发生在关闭互联网连接并重新打开后(当应用程序仍在运行时,无论是否在后台)- Firestore 模块
似乎失去了与服务器的连接,它进行了与预期相反的操作 - 不是停止从缓存中获取数据,而是仅从缓存中获取数据em>.
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.
例如,调试此代码将始终显示 isFromCache
为 true
并且 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 中禁用离线数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!