Reputation: 23
I have a bunch of objects within Firestore in this format. It worked perfectly when I didn't include the arrays within the objects but now it doesn't seem to be loading at all.
Here is the object:
struct BuildingNew: Identifiable, Hashable, Equatable{
var id : String = UUID().uuidString
var ml : String
var name : String
var address : String
var description : String
var phone : String
var website : String
var imageURL: String
var historicalRelevance : String
var images : [String]
var tags : [String]
}
Finally, here is the view model:
class BuildingViewModelNew: ObservableObject {
@Published var buildings = [BuildingNew]()
private var db = Firestore.firestore()
func fetchData(){
db.collection("buildings_new").addSnapshotListener{ (querySnapshot, error) in
guard let documents = querySnapshot?.documents else {
print ("No buildings found")
return
}
self.buildings = documents.map { (queryDocumentSnapshot) -> BuildingNew in
let data = queryDocumentSnapshot.data()
let ml = data["ID"] as? String ?? ""
let name = data["Name"] as? String ?? ""
let address = data["Address"] as? String ?? ""
let description = data["Description"] as? String ?? ""
let phone = data["Phone"] as? String ?? ""
let website = data["Website"] as? String ?? ""
let imageURL = data["current_image"] as? String ?? ""
let historicalRelevance = data["historicalRelevance"] as? String ?? ""
let images = data["images"] as? [String] ?? [""]
let tags = data["tags"] as? [String] ?? [""]
return BuildingNew(ml: ml, name: name, address: address, description: description, phone: phone, website: website, imageURL: imageURL, historicalRelevance: historicalRelevance,images: images, tags: tags)
}
}
}
}
It's fetching the data in SwiftUI using the .OnAppear function but ever since the arrays were added - it hasn't worked. I've tried using Array < String > rather than [String] but have had no luck.
Upvotes: 0
Views: 183
Reputation: 12375
It's not because of the arrays, it's because your field names don't match. In your code, some field names are uppercased:
let ml = data["ID"] as? String ?? ""
let name = data["Name"] as? String ?? ""
...
However, in your database they are lowercased:
id: "MLid"
name: "daves diner"
Therefore, change the following to lowercase:
let ml = data["id"] as? String ?? ""
let name = data["name"] as? String ?? ""
let address = data["address"] as? String ?? ""
let description = data["description"] as? String ?? ""
let phone = data["phone"] as? String ?? ""
let website = data["website"] as? String ?? ""
Upvotes: 3