This is the current way i’m creating and updating data in Firestore.
- As you can see the “setData” object is not type checked which can result in typos and i wont notice if a change needs to be done when the “Report class” is updated.
class Report {
var reporttitle:String
var reportabout:String
var reportlocation:String
init(data: [String: Any]) {
self.reporttitle = data["reporttitle"] as? String ?? ""
self.reportabout = data["reportabout"] as? String ?? ""
self.reportlocation = data["reportlocation"] as? String ?? ""
}
}
var setData = [
"reporttitle": "title",
"reportabout": "about",
"reportlocation": "location",
] as [String: Any]
Firestore.firestore().collection("reports").addDocument(data: setData) { (err) in
if let err = err {
} else {
//Created...
}
}
var setData = [
"reporttitle": "title",
"reportabout": "about",
] as [String: Any]
Firestore.firestore().collection("reports").document( <reportid> ).updateData(setData) { (err) in
if let err = err {
} else {
//Updated...
}
}
What would be a better way of doing this? That includes type-checking
- (Creating) – all values from Report class
- (Updating) – only selective values from Report class