繁体   English   中英

cloud firestore 快照是否读取集合中的所有文档?

[英]Do cloud firestore snapshot reads all the documents in the collection?

我正在创建一个聊天屏幕。 我目前正在做的是使用 Streambuilder 来收听“消息”集合并使用 ListView.builder() 显示消息。 下面是我正在使用的代码。

StreamBuilder<QuerySnapshot>(
            stream: _fireStoreInstance
        .collection('$collectionName/$docID/messages')
        .orderBy('sentAt', descending: true)
        .snapshots(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting)
                return Center(
                  child: CircularProgressIndicator(),
                );
              List<Map> documents = snapshot.data.docs
                  .map((doc) => {'documentId': doc.id, ...doc.data()})
                  .toList();

              return ListView.builder(
                cacheExtent: MediaQuery.of(context).size.height,
                reverse: true,
                itemCount: documents.length,
                padding:
                    const EdgeInsets.only(left: 15.0, right: 15.0, bottom: 5.0),
                itemBuilder: (context, index) {
                  
                  return MessageBubble(
                    ...
                  );
                },
              );
            },
          ),

我担心的是,查询会一次获取集合中的所有文档吗? 如果是那么每次执行查询都会有很多读取

_fireStoreInstance
        .collection('$collectionName/$docID/messages')
        .orderBy('sentAt', descending: true)
        .snapshots();

我需要使用限制来分页吗? 如果我分页,我该如何收听新消息? 谢谢您的帮助。

是的, .snapshots()将读取并继续收听所有符合查询的文档,如果您想要其中的一个子集,则必须使用.limit()对其进行分页。

我找到了这篇文章,其中包含有关如何使用无限滚动使用 Firestore 执行实时分页的逐步视频。 我认为这正是您要查找的内容,因此我不会发布任何代码,因为您可以按照该示例进行操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM