【发布时间】:2019-09-11 18:23:06
【问题描述】:
如何从 firestore 获取 ids 文档?
现在我从 backend 获得了几个 ids 文档,我需要在 tableview 中显示收到的 ids 文档。
在 firestore 我有这个 ids:
xNlguCptKllobZ9XD5m1
uKDbeWxn9llz52WbWj37
82s6W3so0RAKPZFzGyl6
EF6jhVgDr52MhOILAAwf
FXtsMKOTvlVhJjVCBFj8
JtThFuT4qoK4TWJGtr3n
TL1fOBgIlX5C7qcSShGu
UkZq3Uul5etclKepRjJF
aGzLEsEGjNA9nwc4VudD
dZp0qITGVlYUCFw0dS8C
n0zizZzw7WTLpXxcZNC6
例如,我的 backend 只找到了这个 ids:
JtThFuT4qoK4TWJGtr3n
TL1fOBgIlX5C7qcSShGu
UkZq3Uul5etclKepRjJF
或
aGzLEsEGjNA9nwc4VudD
dZp0qITGVlYUCFw0dS8C
n0zizZzw7WTLpXxcZNC6
我只需要在 tableview 中显示这三个 id。 (但实际上后端返回了 100 多个 id,在下面你可以看到对这些 id 进行疯狂排序)
Backend 将此 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()
}
})
【问题讨论】:
标签: ios swift firebase google-cloud-firestore document