在处理Firestore查询时遇到问题,因为我的代码在这里
let wallpaperRef = Firestore.firestore().collection("wallpaper").order(by: "noOfDownloads", descending: true)
wallpaperRef.getDocuments(completion: { (snap, error) in
if error == nil {
print(snap)
}
})
现在此查询的输出是
Optional(<FIRQuerySnapshot: 0x600000070640>)
Optional(<FIRQuerySnapshot: 0x600000070640>)
Optional(<FIRQuerySnapshot: 0x6000000705c0>)
我想立即进行查询并以可读形式获取数据
答案 0 :(得分:0)
如果对集合运行查询,则得到的结果是QuerySnapshot
,其中包含(可能)多个文档。要获取每个文档,您需要遍历结果。来自Firebase documentation on reading multiple documents:
db.collection("cities").whereField("capital", isEqualTo: true)
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
}
}
}
因此,您的代码突出了该else
块中的循环。像这样:
wallpaperRef.getDocuments(completion: { (snap, error) in
if error == nil {
print(snap)
} else {
for document in snap!.documents {
print("\(document.documentID) => \(document.data())")
}
}
})