Until now, I stored the categories data in an array and displayed them.
var categories: [BusinessCategory] = [
.init(id: "id1", name: "Doctor", image: "https://icon.url"),
.init(id: "id2", name: "Restaurant", image: "https://icon.url"),
.init(id: "id4", name: "Citizen Service", image: "https://icon.url"),
.init(id: "id5", name: "Barber Shop", image: "https://icon.url"),
.init(id: "id6", name: "Corona-Test", image: "https://icon.url")
]
I would like to move this into a Database by using Firestore. After storing the categories in different documents, the format is of course different when using getDocuments()
func getAllCategories(){
database.collection("categories").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.data())")
}
}
}
}
The results of the getDocuments() call is:
["image": https://icon.url, "name": Restaurant, "id": 2]
["image": https://icon.url, "name": Citizen Service, "id": 3]
["image": https://icon.url, "name": Doctor, "id": 1]
["image": https://icon.url, "name": Corona-Test, "id": 5]
["image": https://icon.url, "name": Barber Shop, "id": 4]
Any ideas on how to do it the best way? I need to transform all categories
["image": https://icon.url, "name": Doctor, "id": 1]
into
.init(id: "id1", name: "Doctor", image: "https://icon.url"),
.init(id:appears to be a function call which would indicate your array is populated with dynamic data using that function. You can't directly store it in that way. But your document can certainly have fields for that data; id, name and image. Have your reviewed Adding Data and then Reading Data from Firestore using those fields? (hint: one option is for each array element would/could be a separate document in a collection)