获取firestore的几个ids文档

Дми*_*аев 2 document ios firebase swift google-cloud-firestore

如何从firestore获取ids 文档

现在我从后端获取了几个ids 文档,我需要在tableview中显示收到的 ids 文档。

firestore我有这个ids

xNlguCptKllobZ9XD5m1 uKDbeWxn9llz52WbWj37 82s6W3so0RAKPZFzGyl6 EF6jhVgDr52MhOILAAwf FXtsMKOTvlVhJjVCBFj8 JtThFuT4qoK4TWJGtr3n TL1fOBgIlX5C7qcSShGu UkZq3Uul5etclKepRjJF aGzLEsEGjNA9nwc4VudD dZp0qITGVlYUCFw0dS8C n0zizZzw7WTLpXxcZNC6

例如我的后端只找到这个ids

JtThFuT4qoK4TWJGtr3n TL1fOBgIlX5C7qcSShGu UkZq3Uul5etclKepRjJF

或者

aGzLEsEGjNA9nwc4VudD dZp0qITGVlYUCFw0dS8C n0zizZzw7WTLpXxcZNC6

我只需要在tableview中显示这三个 id 。(但实际上后端返回了 100 多个 id,下面你可以看到对这些 id 的疯狂排序)

后端将此 ids 附加到临时数组中var tempIds: [String] = []

那么我如何才能从firestore获取这些 id 并在tableview中显示它们?

我使用这段代码:

fileprivate func query(ids: String) {
    Firestore.firestore().collection(...).document(ids).getDocument{ (document, error) in
        if let doc = document, doc.exists {
            if let newModel = Halls(dictionary: doc.data()!, id: doc.documentID) {
                self.halls.append(newModel)
                self.halls.shuffle()
                self.halls.sort(by: { $0.priority > $1.priority })
                self.tableView.reloadData()
            } else {
                fatalError("Fatal error")
            }
        } else {
            return
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要在后台处理来自后端的id ,并且在处理后需要在tableview中显示处理后的 id ,而无需疯狂排序。

可能需要使用addSnapshotListened,但我不明白如何使用。

更新的代码:

for id in idsList {
                            dispatchGroup.enter()
                            Firestore.firestore().collection(...).document(id).getDocument{ (document, error) in
                                if let doc = document, doc.exists {
                                    if let newHallModel = Halls(dictionary: doc.data()!, id: doc.documentID) {
                                        self.tempHalls.append(newHallModel)
                                        dispatchGroup.leave()
                                    } else {
                                        fatalError("Fatal error")
                                    }
                                } else {
                                    print("Document does not exist")
                                    MBProgressHUD.hide(for: self.view, animated: true)
                                    return
                                }
                            }
                        }

                        dispatchGroup.notify(queue: .global(qos: .default), execute: {

                            self.halls = self.tempHalls

                            DispatchQueue.main.async {
                                MBProgressHUD.hide(for: self.view, animated: true)
                                self.tableView.reloadData()
                            }
                        })
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ste*_*ros 5

您可以使用“IN”查询通过 1 个请求获取 10 个文档,而不是逐一获取文档:

userCollection.where('uid', 'in', ["1231","222","2131"]);

// or 
myCollection.where(FieldPath.documentId(), 'in', ["123","456","789"]);

// previously it was 
// myCollection.where(firestore.FieldPath.documentId(), 'in', ["123","456","789"]);

Run Code Online (Sandbox Code Playgroud)

Firestore 文档:“使用 in 运算符通过逻辑 OR 组合同一字段上最多 10 个等式 (==) 子句。in 查询返回给定字段与任何比较值匹配的文档”