从我的 Firestore 数据库
中删除数据后,我的 Android 应用
需要一些时间才能意识到数据已被删除,我认为这是由于自动数据缓存造成的。我的应用程序与离线使用无关,我想禁用此功能...
我在自定义的 Application 类
中添加了以下内容:
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模块
似乎与服务器失去连接,并且执行的操作与预期相反- 它会从缓存中获取数据而非停止从缓存中获取数据。
例如,调试此代码将始终显示isFromCache
为true
,同时documentSnapshot
为空(尽管在服务器
端不为空):
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
中还有其他禁用数据缓存的方法吗?
编辑:
添加以下代码:FirebaseFirestore.setLoggingEnabled(flase);
(替换上面的代码)到自定义的Application Class
中,可获得相同的结果。