【发布时间】:2020-07-31 15:28:39
【问题描述】:
例如,如果我有一个 QuerySnapshot 类型的 Stream 订阅文档集合,现在将新文档添加到集合中,Stream 是只读取新文档还是重新读取整个文档收藏?
【问题讨论】:
标签: flutter google-cloud-firestore stream
例如,如果我有一个 QuerySnapshot 类型的 Stream 订阅文档集合,现在将新文档添加到集合中,Stream 是只读取新文档还是重新读取整个文档收藏?
【问题讨论】:
标签: flutter google-cloud-firestore stream
我想这就是你要问的。如果您订阅了 Stream 并获得了 Stream 或 QuerySnapshots,您可以选择在每次添加或更改新文档时重新加载整个数据库,或者只获取新文档。
//Just gets new documents
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection(//Collection).snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> asyncSnapshot) {
if (asyncSnapshot.hasData) {
//This is the difference
List<DocumentChange> snapshot =
asyncSnapshot.data.documentChanges;
snapshot.forEach((DocumentChange change) {}
//Get all documents
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('Test').snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> asyncSnapshot) {
if (asyncSnapshot.hasData) {
//This is the difference
List<DocumentSnapshot> snapshot =
asyncSnapshot.data.documents;
snapshot.forEach((DocumentSnapshot snapshot) {
DocumentChange
自上次快照以来更改的文档数组。如果这是第一个快照,所有文档都将在列表中作为已添加更改。
DocumentSnapshot
每次添加或更改新文档时获取所有文档的列表.
请记住,就像@Doug Stevenson 所说的,您需要为每个添加或更改的文档付费。
【讨论】:
DocumentChange 会减少文档读取次数,因为您只阅读新的或更新的文档
每个新添加或更改的文档都需要再次阅读。
【讨论】: