I can’t sadly share something that’s working, but I think this is the better approach.
I worked on my function in ViewModel, as I only need 1 document at the time and it has only 2 values in there. It was previously based on the solution from this thread.
I followed this guide here to write it: Mapping Firestore Data in Swift - The Comprehensive Guide | Peter Friese
Although I watched the dictionary videos here on CWC again, I don’t understand how to declare dataCare right on the 2nd line as a dictionary that uses the struct CareData in my Model. Items is an array. But what does a Dictionary look like? [CareData:CareData]()
didn’t work like a bunch of other things I tried in my cluelessness.
@Published var itemList = [Item]()
@Published var careData = [CareData]() // <- Should be a dictionary
func getCareData(item: String) {
let collectionCare = database.collection("collection-name").document(item)
// Get the document
collectionCare.getDocument { document, error in
if error != nil {
print("Error")
} else {
if let document = document {
do {
self.careData = try document.data(as: CareData.self)
} catch {
print(error)
}
}
}
}
}
My Model got a little upgrade following the guide, too.
struct CareData {
@DocumentID var id: String?
var avHeight: Int
var avWater: Int
}