3

I'm trying to figure out the best way to enable Firestore persistence and adjust the cache size using AngularFire.

Firebase documentation suggest we do it like this:

firebase.firestore().settings({
     cacheSizeBytes: firebase.firestore.CACHE_SIZE_UNLIMITED
});

AngularFire does not mention adjusting such settings, but digging into the code it looks like you can pass an object of type "firebase.firestore.PersistanceSettings" in as such:

AngularFirestoreModule.enablePersistence({
      experimentalForceOwningTab: true,
      synchronizeTabs: true,
}),

However, those are the only two properties allowed in the PersistanceSettings type.

I tried creating a constructor in app.module.ts and doing things according to the Firebase documentation but I'm not even sure if it is working.

export class AppModule {
  constructor() {
    firebase.firestore().settings({
      cacheSizeBytes: firebase.firestore.CACHE_SIZE_UNLIMITED,
    });
  }
}

How do I check what the cache size settings are at run time? Am I doing it right?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Methodician
  • 2,396
  • 5
  • 30
  • 49

3 Answers3

0

CACHE_SIZE_UNLIMITED is -1, to be set preferably in app.module.ts:

const settings: firebase.firestore.Settings = {
  cacheSizeBytes: firebase.firestore.CACHE_SIZE_UNLIMITED
};
firebase.firestore().settings(settings);

There doesn't seem to be a method to read the settings... cacheSizeBytes is already known.
And one probably cannot change the value of cacheSizeBytes either, once having connected.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Right, I got that in my question. You only variablized and strongly typed the parameter. The question is, in an AngularFire project is the app.modlue.ts constructor the right/preferable place to do this? Is there a more AngularFire way to do it? – Methodician Oct 21 '21 at 20:26
0

Searching around the AngularFire documentation, I found their document related to offline storage. It explains how to enable file persistence in your app and also that you can test whether a document is retrieved from cache or Firestore using the fromCache property.

To check whether you are receiving data from the server or the cache, use the fromCache property on the SnapshotMetadata in your snapshot event. If fromCache is true, the data came from the cache and might be stale or incomplete. If fromCache is false, the data is complete and current with the latest updates on the server.

As for setting the cache size for Firestore, it appears that the only way is what you have been trying and it is a variable set in the Firestore instance properties. Here are full samples from the documentation related to setting the cache size and enabling persistence.

ErnestoC
  • 2,660
  • 1
  • 6
  • 19
-1

I was having the same issue but found a way of setting it on GitHub.

import { AngularFirestoreModule, SETTINGS } from '@angular/fire/firestore';

@NgModule({
  imports: [
    AngularFirestoreModule,
    AngularFireAuthModule,
    AngularFireStorageModule,
    AngularFireModule.initializeApp(environment.firebase),
 ],
 providers: [
 {
   provide: SETTINGS,
   useValue: { ignoreUndefinedProperties: true, cacheSizeBytes: 1048576 },
 };
]
Calvin
  • 347
  • 1
  • 4
  • 18