ios - 获取多个 ids 文档 firestore

标签 ios swift firebase google-cloud-firestore document

如何从 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)

后端 将此 id 附加到临时数组 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
        }
    }
}

我需要在后台处理来自backend 的 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()
                            }
                        })

enter image description here

最佳答案

与其逐一获取文档, 您可以使用“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"]);

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

关于ios - 获取多个 ids 文档 firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55796427/

相关文章:

ios - "Stack"UIViewController 中的 UIViews (iOS 7)

ios - 针对 iPhone 中的不同屏幕改变 UITextFields 的高度和间距

arrays - 类型 '[Double]?' 的值没有成员 'append'

ios - 防止 RxSwift 中的冗余操作

javascript - 保护密码客户端 Firebase

ios - 获取 GeoFire 中特定范围内的所有对象

javascript - Firebase react 绑定(bind)

ios - 未采用扩展的 Swift 段错误

ios - window.location.href 在phonegap 中不起作用

ios - 如何从 iOS 中的流接收数据 (swift)