How does Firestore persistence really work?(Firestore 持久性如何真正发挥作用?)
问题描述
我在
A
我昨天添加了这个文档,并且在连接设备的情况下正常阅读.
今天我可以使用设备离线阅读此文档.
B
- 昨天我也添加了这个文件.设备在此文档存在后已在线,但我没有阅读此文档.已添加但从未阅读.
- 今天,设备离线我尝试阅读此文档,但不可行.(为什么设备在线时没有同步?)
C
昨天我访问了集合6
,文档03000503
...
没有为整个集合启用持久化?
当我添加文档03030501
时,该文档在线时不与设备同步?如果我不阅读文档在线一次,不同步?在这种情况下,不是collection 6中的所有文档都同步?
如果我将一个文档添加到集合 6 中,我需要设备在该文档在线时同步该文档,而无需输入该新文档.这可能吗?
这是读取文档的代码:
public void launchFirestore() {最终 FirebaseFirestore db = FirebaseFirestore.getInstance();字符串 fechaDD = strFechaHoy.substring(6, 8);字符串 fechaMM = strFechaHoy.substring(4, 6);字符串 fechaYYYY = strFechaHoy.substring(0, 4);DocumentReference calRef = db.collection(CALENDAR_PATH).document(fechaYYYY).collection(fechaMM).document(fechaDD);calRef.addSnapshotListener(new EventListener() {@覆盖public void onEvent(@Nullable DocumentSnapshot calSnapshot,@Nullable FirebaseFirestoreException e) {DocumentReference dataRef=calSnapshot.getDocumentReference(VISPERAS_ID);if (e != null || dataRef==null) {发射Volley();返回;}if (calSnapshot != null && calSnapshot.exists()) {dataRef.get().addOnSuccessListener(new OnSuccessListener() {@覆盖公共无效 onSuccess(文档快照数据快照){//Log.d(TAG,"---"+dataSnapshot.toString());mBreviario = dataSnapshot.toObject(Breviario.class);显示数据();}});} 别的 {发射Volley();}}});}
Firebase 不会自动缓存设备上数据库中的所有数据.它的磁盘持久性仅将客户端已经请求的数据存储在其本地数据库中.
来自您链接的同一文档页面:
<块引用>此功能会缓存一份您的应用正在使用的 Cloud Firestore 数据副本,以便您的应用可以在设备离线时访问数据.
如果您考虑一下,这是有道理的:如果 Firebase 会同步您的用户可能看到的所有数据,那么它可能必须同步整个数据库.这在移动设备上是不合理的.
如果您希望特定数据可离线使用,请确保在设备仍在线时将观察附加到该数据.
I read this in the 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
.
In Android, i create my Firestore reference as follows:
final FirebaseFirestore db = FirebaseFirestore.getInstance();
Then, I assume persistence is enabled by default.
I will try to explain with this image what's happening:
A
I added this document yesterday and i readed this normally with the device connected.
Today i can read this document with the device off-line.
B
- Yesterday I added this document too. The device was on-line after the existence of this document but i don't readed this document. It was added but never read.
- Today, with the device off-line i try to read this document, but is not possible. (Why was not synched during the device was on-line?)
C
Yesterday I haved access to collection 6
, document 03000503
...
The persistence is not enabled for the entire collection?
When I add the document 03030501
, this document is not synchronized with the device when it's on-line? If i don't read the document one time on-line there is not synchronization? In this case the synchronization is not for all documents in collection 6?
I need that if I add one document to collection 6 the device synchronize this document when it's on-line without having to enter in that new document. This is possible?
Here is the code for read the documents:
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 does not automatically cache all data from your database on the device. Its disk persistence only stores data that the client is already requesting in its local database.
From the same documentation page you linked:
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.
If you think about it, that makes sense: if Firebase would sync all data that your user can possibly see, it would have to synchronize potentially the entire database. That isn't reasonable on a mobile device.
If you want specific data to be available offline, make sure to attach an observe to that data while the device is still online.
这篇关于Firestore 持久性如何真正发挥作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Firestore 持久性如何真正发挥作用?
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01