如果启用了持久性,AngularFire不会检测到更改 [英] AngularFire doesn't detect changes if persistence is enabled

查看:58
本文介绍了如果启用了持久性,AngularFire不会检测到更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AngularFire ,并且在我的app.module.ts中启用了对脱机支持的持久性:

I'm using AngularFire and have the persistence for offline support enabled in my app.module.ts:

imports: [
   AngularFireModule.initializeApp(environment.firebase),
   AngularFirestoreModule.enablePersistence()
]

应用程序启动时,我使用valueChanges()检索用户,如下所示:

When my application start, I retrieve the user using valueChanges() like the following:

const doc: AngularFirestoreDocument<User> = this.myCollection.doc<User>(userId);

doc.valueChanges().pipe(take(1)).subscribe((user: User) => {
    console.log(user);
}, (err) => {
    console.err(err);
});

对我来说,很奇怪,如果我将其他来源的应用程序对数据库进行了修改,例如,如果我将自己登录到Google Firestore控制台并手动修改了用户数据中的值,则不会提取这些数据

Strangely to me, if I applies modifications to the database from another source as the application, for example if I log my self into the Google Firestore Console and modify manually a value in the user data, these data won't be fetched.

如果我重新启动应用程序,它将始终获取本地保留的数据,并且永远不会注意到值已更改.

If I restart the app it will always fetch the locally persisted data and will never notice that the values were changed.

如果分别删除enablePersistence()的持久性,则将正确提取数据.

If I remove the persistence, respectively the enablePersistence(), the data are correctly fetched.

您知道问题的原因是什么吗?

Do you know what's the reason of the problem?

在这种情况下我应该不使用valueChanges()吗?

Should I not use the valueChanges() in such case?

还是会发生这种情况,因为我只获取了一次值(请参见take(1)),因此从未获取过更新的值吗?

Or does this happens because I fetch the value only once (see take(1)) and therefore the updated value is never fetched?

提前寻求帮助

推荐答案

5分钟后...正是我的问题是我不监听更改,而只读取一个值(take(1))本地持久化的将被读取,此后不再读取,因此不会获取更新的值

5 minutes later...exactly my problem is the fact that I'm not listening for changes and only reading one value (take(1)), respectively the locally persisted one will be read and nothing more afterwards, therefore the updated value won't be fetched

这篇关于如果启用了持久性,AngularFire不会检测到更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆