Ionic 离子Angular - Firebase离线缓存两次返回一次数据

s71maibg  于 2022-12-08  发布在  Ionic
关注(0)|答案(1)|浏览(177)

Description of the initial situation:

I have enabled data persistence on Firestore by following the documentation: https://firebase.google.com/docs/firestore/manage-data/enable-offline It works, when I make an offline write request my listener receives the information and I can display it locally. When I get back to the connection the data are well synchronized.

Description of the problem:

  • I open the application on android and go offline.
  • I make an offline modification to firestore DB.
  • I kill my application.
  • I reconnect, still offline.
  • I try to read the data from the cache.
  • One time out of two : I get the data, it works perfectly.
  • The other time : The cache is like empty and doesn't return any data.

When I say one out of two times, it means that when I kill the app and don't get the data, if I kill it again and open it, I get the data again (without being in online mode). If I do it again, I lose them. (And this boucle is infinite)
The request to get the data is the same each time.
PS : On PWA version, it work every time

Question: How do I get the data from the cache every time? Did I miss a step?

2lpgd968

2lpgd9681#

我终于找到了解决办法:
我使用AngularFire以这种方式启用持久性(在app.module.ts中):

@NgModule({
    declarations: [AppComponent],
    imports: [
        ...
        AngularFireModule.initializeApp(firebaseConfig),
        AngularFirestoreModule.enablePersistence({synchronizeTabs: true}),
        ...
    providers: [
        ...
    ],
    bootstrap: [AppComponent],
})

必须启用选项(synchronizeTabs)。我的猜测是,应用程序没有被Android完全杀死,因此在下次打开时被算作第二个选项卡。
接下来,我这样使用它:对于每个请求,我检查用户是否在线,如果不在线,我切换到离线模式:

private async disableNetWorkIfOffline() {
    if (!this.networkService.isOnline()) {
        await this.afs.firestore.disableNetwork();
    } else {
        await this.afs.firestore.enableNetwork();
    }
}
  • 是我的一个服务
  • 我以为我从文档中了解到这是自动完成的,但由于某种原因,这不是我的情况,所以我手动完成

相关问题