collection$.valueChanges() return 是只改变还是整个集合?
Does collection$.valueChanges() return only changes or the whole collection?
我不太清楚 valueChanges 的实际作用——如果我的订阅是这样的:
(Typescript/Angular)
this.accountsCollection = this.firestore.collection(`budgets/${budgetId}/accounts`);
this.accounts$ = this.accountsCollection.valueChanges();
this.accountsSubscription = this.accounts$.subscribe(accountData => {
AccountsManager.getInstance().loadData(accountData)
})
我是要在每次更新、添加、删除等集合文档时获取整个集合,还是只获取更改的文档。
如果我只想查看订阅中的更改 (added/updated/deleted),如何获得能做到这一点的订阅?
每当添加、更改或删除任何文档时,您的回调将收到整套文档。只要侦听器处于活动状态,文档集就会缓存在内存中,并且不需要每次都传输每个文档 - 实际上仅通过连接发送增量。
我建议通读 streaming collection data 的文档。
在您的代码中,如果您想知道实际更改了哪些文档,valueChanges
无法帮助您识别。正如文档所述:
Why would you use it? - When you just need a list of data. No document metadata is attached to the resulting array which makes it simple to render to a view.
您应该改用 snapshotChanges()
。它将为您提供 DocumentChangeAction 类型的对象,您可以使用这些对象来告诉您实际更改了哪些文档。
我不太清楚 valueChanges 的实际作用——如果我的订阅是这样的:
(Typescript/Angular)
this.accountsCollection = this.firestore.collection(`budgets/${budgetId}/accounts`);
this.accounts$ = this.accountsCollection.valueChanges();
this.accountsSubscription = this.accounts$.subscribe(accountData => {
AccountsManager.getInstance().loadData(accountData)
})
我是要在每次更新、添加、删除等集合文档时获取整个集合,还是只获取更改的文档。
如果我只想查看订阅中的更改 (added/updated/deleted),如何获得能做到这一点的订阅?
每当添加、更改或删除任何文档时,您的回调将收到整套文档。只要侦听器处于活动状态,文档集就会缓存在内存中,并且不需要每次都传输每个文档 - 实际上仅通过连接发送增量。
我建议通读 streaming collection data 的文档。
在您的代码中,如果您想知道实际更改了哪些文档,valueChanges
无法帮助您识别。正如文档所述:
Why would you use it? - When you just need a list of data. No document metadata is attached to the resulting array which makes it simple to render to a view.
您应该改用 snapshotChanges()
。它将为您提供 DocumentChangeAction 类型的对象,您可以使用这些对象来告诉您实际更改了哪些文档。