我在 documentation 中读到了这篇文章:
To use offline persistence, you don't need to make any changes to the code that you use to access Cloud Firestore data. With offline persistence enabled, the Cloud Firestore client library automatically manages online and offline data access and synchronizes local data when the device is back online.
...
For Android and iOS, offline persistence is enabled by default. To disable persistence, set the
PersistenceEnabled
option tofalse
.
在 Android 中,我创建我的 Firestore 引用如下:
final FirebaseFirestore db = FirebaseFirestore.getInstance();
然后,我假设默认情况下启用持久性。我将尝试用这张图片解释发生了什么:
一个
乙
C
昨天我访问了收藏
6
, 文件 03000503
...未启用持久性整个系列 ?
当我添加文档
03030501
,该文档上线时不与设备同步?如果我不阅读文档 一次上线有没有同步?在这种情况下,同步并不适用于 中的所有文档。收藏 6 ?我需要,如果我将一个文档添加到集合 6,则设备在联机时同步此文档,而无需输入该新文档。这个有可能?
以下是阅读文档的代码:
public void launchFirestore() {
final FirebaseFirestore db = FirebaseFirestore.getInstance();
String fechaDD = strFechaHoy.substring(6, 8);
String fechaMM = strFechaHoy.substring(4, 6);
String fechaYYYY = strFechaHoy.substring(0, 4);
DocumentReference calRef = db.collection(CALENDAR_PATH).document(fechaYYYY).collection(fechaMM).document(fechaDD);
calRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot calSnapshot,
@Nullable FirebaseFirestoreException e) {
DocumentReference dataRef=calSnapshot.getDocumentReference(VISPERAS_ID);
if (e != null || dataRef==null) {
launchVolley();
return;
}
if (calSnapshot != null && calSnapshot.exists()) {
dataRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot dataSnapshot) {
//Log.d(TAG,"---"+dataSnapshot.toString());
mBreviario = dataSnapshot.toObject(Breviario.class);
showData();
}
});
} else {
launchVolley();
}
}
});
}
最佳答案
Firebase 确实 不是 自动缓存设备上数据库中的所有数据。它的磁盘持久性仅将客户端已经请求的数据存储在其本地数据库中。
从您链接的同一文档页面:
this feature 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.
如果您考虑一下,这是有道理的:如果 Firebase 会同步您的用户可能看到的所有数据,那么它可能必须同步整个数据库。这在移动设备上是不合理的。
如果您希望特定数据可离线使用,请确保在设备仍在线时将观察附加到该数据。
关于android - Firestore 持久性如何真正发挥作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55590838/