【问题标题】:Billable Reads with snapshotChanges()使用 snapshotChanges() 进行计费读取
【发布时间】:2019-03-01 12:13:32
【问题描述】:

我正在使用 Cloud Firestore,当我将文档添加到我的一个根集合并在该集合上记录 snapshotChanges() 的结果时,日志会显示该集合中的 每个 文档。

我了解 Firebase 根据返回的文档(例如查询中的文档)收费。

snapshotChanges() 是否也会导致对每个文档进行计费读取?

firebase.service.ts

@Injectable()
export class FirebaseService {

  public recordsCollection: AngularFirestoreCollection<any[]>;
  public records$: Observable<any[]>;

  public financialsCollection: AngularFirestoreCollection<any[]>;
  public financials$: Observable<any[]>;

  constructor(private db: AngularFirestore) {

    this.recordsCollection = this.db.collection<any[]>('records');
    this.records$ = this.mapAndReplayCollection(this.recordsCollection);

    this.financialsCollection = this.db.collection<any[]>('financials');
    this.financials$ = this.mapAndReplayCollection(this.financialsCollection);

  }

  private mapAndReplayCollection(collection: AngularFirestoreCollection<any[]>): any { 
   return collection.snapshotChanges()
      .pipe(
        map(changes => {
          return changes.map(a => {
           console.log(`${collection.ref.id} snapshotChanges: ${a.payload.doc.id}`);
           return { realId: a.payload.doc.id, ...a.payload.doc.data() }
          })
        }),
       shareReplay(1),
    );
  }

}

【问题讨论】:

  • shareReplay 将保存第一次发射并始终发射最后一个结果,就像行为主题一样。
  • 谢谢@FanCheung。修改后的问题仅关注snapshotChanges() 关于计费阅读。

标签: angular firebase rxjs google-cloud-firestore


【解决方案1】:

认为您的方法是正确的,排放都来自您的现有集合实例(热可观察)。但我建议只是扩展金融集合链以创建financial$financialsWithReplay$ 流以看起来更明确,而不是将它们包装在函数中(这可能就是为什么你不确定它是否缓存了最后一个结果)

this.financials$ = this.financialsCollection.snapshotChanges()
  .pipe(
    map(changes => {
      return changes.map(a => {
       console.log(`${collection.ref.id} snapshotChanges: ${a.payload.doc.id}`);
       return { realId: a.payload.doc.id, ...a.payload.doc.data() }
      })
    })
);

this.financialsWithReplay$=this.financials$.pipe(shareReplay(1))

【讨论】:

    猜你喜欢
    • 2015-12-22
    • 2018-06-16
    • 2018-02-27
    • 2015-08-09
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2017-10-27
    相关资源
    最近更新 更多