Dynamically set enablePersistence() with angularfire2
Currently I'm trying to dynamically set enablePersistence depending on the users platform. Given your issue advice I have posted a question on stack overflow. From that answer I have gleamed it's possible with the native library but i'm not sure if it's possible with angularfire2.
Here's what I'm doing.
//app.component.ts
export class MyApp {
constructor() {
this.initializeApp();
}
initializeApp() {
AngularFirestoreModule.enablePersistence();
}
}
//app.module.ts
...
providers: [
AngularFirestore,
...
]
})
This seems to be throwing no errors, but i'm not sure i'm doing this correctly.
Thanks in advance!
From what I've seen you need to enable persistence in app.module.ts not in the component.
//app.module.ts
@NgModule({
imports: [
BrowserModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFirestoreModule.enablePersistence(),
],
from https://github.com/angular/angularfire2/blob/master/docs/firestore/offline-data.md To test it out i would recommend loading images in 2 different components navigating between them then disabling your internet and seeing if the images still load while making sure your cache is disabled.
As for the platform, offline persistence is enabled by default on ios and android and disabled on the web. So you can either explicitly enable for all or disable for all or leave the default configurations. There may be more options too.
Ah so it seems I actually don't need to add anything in order to achieve the behavior I want.
Your saying if I just leave off AngularFirestoreModule.enablePersistence() ios and Android will automatically still have persistence enabled, but the web won't. Is that correct?
Correct , but i would still test it to make sure it's working as intended.
I've confirmed via testing @anasbaddyr just in case anyone else finds there way to this thread.
Hi Guys,
In ionic 4 , I had enable offline persistence in app.module.ts file. But not synchronising updated the documents data, when if i do continuously updating in Offline and Online device.
what is I'm trying: Trying to updating a document multiple time with the last status of updated a document data. Its works fine , if i do update a document in online device or offline device.
that doesn't really answer the question in the title though. Where is the stack overflow question I can't find it? Also how do I set enablePersistence dynamically in angular? I don't want it enabled when developing locally, it's confusing. This seems to work:
import { AngularFirestore } from '@angular/fire/firestore';
constructor(
private afs: AngularFirestore
) {}
if (environment.production) {
try {
const firestore = this.afs.firestore;
firestore
.enablePersistence()
.then(() => console.log('Offline support enabled'))
.catch((error) => {
console.warn('Offline support not available');
});
} catch (error) {
console.warn('Offline support not available');
}
}