使用交换流时,如何处理限制为10的FireStore? [英] How can I deal with the firestore whereIn limit of 10 when using a switch stream?

查看:8
本文介绍了使用交换流时,如何处理限制为10的FireStore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Stream,它接受一个组的Stream并返回其成员的Stream。我使用切换映射从组快照中获取成员。

但是我有以下问题。我使用带有whereIn过滤的WHERE查询。但问题是,根据FireStore文档,whereIn只能接受10个或更少条目的列表

限制请注意和的以下限制 ARRAY-CONTAINS-ANY:

在AND ARRAY-CONTAINS-ANY中,最多可支持10个比较值。

https://firebase.google.com/docs/firestore/query-data/queries#limitations

所以我在处理这位参议员时遇到了一些困难。

  Stream<List<UserModel>> groupMembersStream(Stream<GroupModel> groupStream) {
    return groupStream.switchMap(
      (value) => _fireStore
          .collection(APIRoutes.users)
          .where(FieldPath.documentId, whereIn: value.members.keys.toList(growable: false))
          .snapshots()
          .map((snapshot) =>
              snapshot.documents.map((document) => UserModel.fromFirestore(document)).toList(growable: false)),
    );
  }

因为我首先需要组成员id,所以我需要一个switchMap。因此,我不能简单地拆分组成员列表,然后对10个id的每个块进行单独查询。

那么我该如何处理此问题?

推荐答案

我最终是这样做的

  Stream<List<UserModel>> groupMembersStream(Stream<GroupModel> groupStream) {
    return groupStream.switchMap(
      (value) => _chunckSizeGroupMembersStream(value),
    );
  }

  Stream<List<UserModel>> _chunckSizeGroupMembersStream(GroupModel group) {
    final List<List<String>> memberChunks = chunkSizeCollection(group.members.keys.toList(growable: false), 10);
    List<Stream<List<UserModel>>> streams = List<Stream<List<UserModel>>>();
    memberChunks.forEach((chunck) => streams.add(_fireStore
        .collection(APIRoutes.userCollection)
        .where(FieldPath.documentId, whereIn: chunck)
        .snapshots()
        .map((snapshot) =>
            snapshot.documents.map((document) => UserModel.fromFirestore(document)).toList(growable: false))));
    return ZipStream(streams, (value) => value.last);
  }

这篇关于使用交换流时,如何处理限制为10的FireStore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆