How does Firestore persistence really work?
Asked Answered
L

1

6

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 created my Firestore reference as follows:

final FirebaseFirestore db = FirebaseFirestore.getInstance();

I assume persistence is enabled by default.

I will try to explain with this image what's happening:

Console view of database

Letter A

  • I added this document yesterday and I could read it normally with the device online.
  • Today I can read this document with the device offline.

Letter B

  • Yesterday I added this document too. The device was online AFTER the existence of this document but I did not read it. It was added but never read.
  • Today, with the device offline, I tried to read this document, but I could not. (Why was it not synced while the device was online?)

Letter C

  • Yesterday I had access to collection 6, document 03000503, but persistence is not enabled for the entire collection?
  • So when I added the document 03030501, this document was not synchronized with the device when it was online? If I don't read the document one time while online there is not synchronization, and synchronization is not for all documents in collection 6?
  • Is it possible for collection 6 to synchronize when I add a document while online?

Here is my code for reading 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();
            }
        }
    });
}
Lemuroid answered 9/4, 2019 at 10:36 Comment(0)
S
5

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.

Supple answered 9/4, 2019 at 13:44 Comment(3)
Thanks for your response, it's more clair now. As you say, If you want specific data to be available offline, make sure to attach an observe to that data while the device is still online. it's just I need for all the content into my lh collection. How I can do that? It would not be very expensive?Lemuroid
If you want a database that fully replicates offline, you might want to consider other options indeed.Supple
Yes, in the future the idea is to create an SQLite database in the app, but temporarily I need an easy way to offer that part of the data offline, then I think in Firebase. It is not all my database, but the content that is in the collection lh only.Lemuroid

© 2022 - 2024 — McMap. All rights reserved.