I successfully get a document from Firestore.
The next question is how I should change the code, so that i get the latest Document that was added to the Collection.
I know that I can use some arguments like .order(by: String)
or .limit(to: Int)
, but I don't know WHERE and HOW to use it.
I also looked after an answer in the Firestore Documentation, but there is just said how and what function you have to use for some specific examples.
I added the function, that should get this unknown document and a picture of how the Database Model looks like.
Code snippet:
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 (the variable inside the red box, is the variable that i want order by)