在 Angular Firestore 查询中禁用缓存 [英] Disable caching in Angular Firestore queries

查看:21
本文介绍了在 Angular Firestore 查询中禁用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 firestore 查询以获取数据,但该查询之前从缓存的数据查询中返回数据,然后在第二次从服务器中返回其他数据(之前未查询过).有没有办法可以禁用 firestore 查询的缓存,以便每次查询时请求都转到数据库.

I am running a firestore query to get data but the query is returning data from cached data queries earlier and then returns additional data (which was not queried earlier) in the second pass from server. Is there a way I can disable caching for firestore queries so that request goes to DB every time I query something.

this.parts$ = this.db.collection<OrderBom>('OrderBom', ref => {
      let query : firebase.firestore.Query = ref;
      query = query.where('orderPartLC', '==', this.searchValue.toLowerCase());
      return query;
    }).valueChanges();

推荐答案

将那个 .valueChanges() 更改为 .snapshotChanges() 然后你可以申请一个过滤器.请参阅下面的示例.

Change that .valueChanges() to a .snapshotChanges() then you can apply a filter. See the example below.

我不喜欢改变默认行为(默认配置).我认为这是一种理想的行为,好的做法是尽快向用户显示数据,即使您刷新两次屏幕也是如此.

I dont like changing default behavior (default configurations). I saw it's a desired behavior and the good practice is to show the data as soon as possible to the user, even if you refresh twice the screen.

我不认为在我们没有选择时过滤 fromCache === false 是一种不好的做法.(在我的情况下,我在收到第一个请求后做了更多请求,因此由于承诺和其他异步任务"缓存/服务器顺序完全丢失)

I dont think is a bad practice to filter on fromCache === false when we dont have a choise. (In my case I do more requests after i receive this first one so due to promises and other async 'tasks' cache/server order is completly lost )

参见这个已解决的问题

  getChats(user : User) {
    return this.afs.collection<Chat>("chats", 
    ref => ref.where('participantsId', 'array-contains', user.id)
              .snapshotChanges()
              .pipe(filter(c=> c.payload.doc.metadata.fromCache === false)).
              .pipe(map(//probaly want to parse your object here))

  }

这篇关于在 Angular Firestore 查询中禁用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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