如何处理 firestore 查询,因为我将快照结果作为 FIRQuerySnapshot`
How to process firestore query as i am getting snapshot result as FIRQuerySnapshot`
我在处理 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>)
我想获取此查询快照并获取任何初始化为可读形式的数据
如果您 运行 对集合进行查询,您得到的结果是 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())")
}
}
})
我在处理 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>)
我想获取此查询快照并获取任何初始化为可读形式的数据
如果您 运行 对集合进行查询,您得到的结果是 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())")
}
}
})