【发布时间】: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