How does Firestore persistence really work?

2019-08-18 06:21发布

问题:

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();
            }
        }
    });
}

回答1:

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.