【发布时间】:2020-08-27 14:50:26
【问题描述】:
我已成功从 Firestore 获取文档。
下一个问题是我应该如何更改代码,以便获得添加到集合中的最新文档。
我知道我可以使用.order(by: String) 或.limit(to: Int) 之类的一些参数,但我不知道WHERE 和HOW 来使用它。
我还查看了 Firestore 文档中的答案,但只是说明了对于某些特定示例,您必须如何使用以及使用什么功能。
我添加了这个函数,它应该得到这个未知的文档和数据库模型的图片。
代码sn-p:
func getSingleproperty() {
var desiredProperty: String!
let docRef = db.collection("UnKnownErrorMessages").document()
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
//Print all data in the document
print("Document data: \(dataDescription)")
if let allPropertiesInDocument = document.data() {
let nameOfPropertyIwantToRetrieve = "read"
if let selectedProperty = allPropertiesInDocument[nameOfPropertyIwantToRetrieve] {
desiredProperty = selectedProperty as? String
}
}
//Print exact the data that is in 'nameOfPropertyIwantToRetrieve' specified
print("Value of desiredProperty is \(desiredProperty.description)")
} else {
print("Document does not exist \(error.debugDescription)")
}
}
}
Firestore database model(红框内的变量,是我要排序的变量)
【问题讨论】:
-
嘿@tajh,我知道这个文档存在(查找了很多次),但我不知道如何请求这个特定的术语。我一定是做错了,没有注意到他。但是无论如何,我可能没有访问文档。
标签: swift firebase google-cloud-firestore swiftui