Firestore持久性如何真正发挥作用? [英] How does Firestore persistence really work?

查看:60
本文介绍了Firestore持久性如何真正发挥作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在

A

  • 我昨天添加了此文档,并且在连接设备的情况下正常阅读了该文档.

  • 今天,我可以使用设备离线来阅读此文档.

B

  • 昨天我也添加了此文档.该文档存在后,设备已联机,但我没有阅读此文档.它已添加,但从未阅读.
  • 今天,设备离线,我尝试阅读此文档,但无法阅读.(为什么设备在线期间未同步?)

C

昨天我可以访问馆藏 6 ,文档为 03000503 ...

未为整个集合启用持久性吗?

当我添加文档 03030501 时,此文档在线时是否与设备不同步?如果我一次在线不阅读文档,是否没有同步?在这种情况下,同步不是针对集合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< DocumentSnapshot>(){@Override公共无效onEvent(@Nullable DocumentSnapshot calSnapshot,@Nullable FirebaseFirestoreException e){DocumentReference dataRef = calSnapshot.getDocumentReference(VISPERAS_ID);if(e!= null || dataRef == null){launchVolley();返回;}if(calSnapshot!= null&& calSnapshot.exists()){dataRef.get().addOnSuccessListener(new OnSuccessListener< DocumentSnapshot>(){@Override公共无效onSuccess(DocumentSnapshot dataSnapshot){//Log.d(TAG,"---" + dataSnapshot.toString());mBreviario = dataSnapshot.toObject(Breviario.class);showData();}});} 别的 {launchVolley();}}});} 

解决方案

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 to false.

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持久性如何真正发挥作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆