首页 > 解决方案 > 如何查询 Firestore 集合中的所有文档以获取数组中的所有字符串?

问题描述

我有一个 UID 数组,我想在我的根用户集合中查询包含等于我数组中每个 UID 的用户对象的文档。现在我的代码如下所示:

-(void)queryFriendsOfCurrentUser{

    FIRUser *user = [[FIRAuth auth] currentUser];
    NSMutableArray *friendsUIDArray = [[NSMutableArray alloc] init];

        for (NSString *friend in friendsUIDArray){
            [[[self.db collectionWithPath:@"Users"] queryWhereField:@"uid" isEqualTo:friend] getDocumentsWithCompletion:^(FIRQuerySnapshot * _Nullable snapshot, NSError * _Nullable error) {
                if (error != nil) {
                    NSLog(@"Error getting documents: %@", error);
                } else {
                    for (FIRDocumentSnapshot *document in snapshot.documents) {

                        NSDictionary *object = document.data;
                        [self.friendArray addObject:object];
                }
            }
        }];
    }
}

在许多情况下,我的数组可能包含 20 多个 UID。我是否getDocuments()在 for 循环中进行过多的数据库调用?

标签: iosobjective-cgoogle-cloud-firestore

解决方案


推荐阅读