Balazs Fabian
Balazs Fabian

Reputation: 251

Firestore offline cache

I'm building an Android application which has to work offline for weeks, but can sync immediately with a remote DB as it goes online.

My question is can Firestore be a good option for this? How long does Firestore keep its offline cache?

Upvotes: 25

Views: 17190

Answers (3)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Firestore can be configured to persist data for such disconnected/offline usage. I recommend that you read the enable offline persistence section in the docs, which contains this sample of enabling this feature:

FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
        .setPersistenceEnabled(true)
        .build();
db.setFirestoreSettings(settings);

This persistence is actually enabled by default on Android and iOS, so the call above is not needed.

Your Android code that interact with the database will be the same whether you're connected or not, since the SDK simply works the same. If you want to detect whether data is coming from the cache (and thus potentially stale), read the section Listen to offline data in the docs.

The data in the cache does not expire after a certain amount of time. The only two reasons data is removed from the cache:

  1. The data has been removed from the server, in which case the client will remove it from the disk cache.
  2. The client needs to purge its disk cache to make space for more recent data.

Upvotes: 17

Harsh Patalia
Harsh Patalia

Reputation: 173

EDIT-25/5/2020: Francesco is correct, the docs link given in the comment does clarify that. It seems the cache size has been changed, by default it has been decreased to 40MB.

OLD: The following answer follows the official guide in the following link:

Handling Cache size

FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
    .setCacheSizeBytes(FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED)
    .build();
db.setFirestoreSettings(settings);

The above code has a flag set for setCacheSize(), which will prevent your cache from being cleared. You can also specify the same in size. If you do not set this by default the size is 100MB.

As per the guide, there is a method to check if the data you query came from cache or the firestore. Also the moment your device is back online the firestore refreshes the cache, and keeps the data synchronized.

To answer your question, as you have to work with offline data for weeks, i suggest every time the data is fetched to store it in json/xml formats, as storing huge amount of data in cache is not a really good approach when thought of in terms of performance.

I hope i helped you clear some things out.

Upvotes: 3

Sam Stern
Sam Stern

Reputation: 25134

If you listen to data in Cloud Firestore, you will get immediate snapshots of cached data and also updates when your app is able to connect online:

final DocumentReference docRef = db.collection("cities").document("SF");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(@Nullable DocumentSnapshot snapshot,
                        @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "Listen failed.", e);
            return;
        }


        // Determine if the data came from the server or from cache
        String source = snapshot != null && snapshot.getMetadata().hasPendingWrites()
                ? "Local" : "Server";


        // Read the data
        if (snapshot != null && snapshot.exists()) {
            Log.d(TAG, source + " data: " + snapshot.getData());
        } else {
            Log.d(TAG, source + " data: null");
        }
    }
});

Persistence is enabled by default so this behavior does not require any configuration.

Upvotes: 2

Related Questions