I am creating a order system, and for that, I am storing the cart of a user in Firestore with maps, so the collection will be called "carts", then the document will be saved as the user's UID and inside of the document there should be maps with what he ordered, the quantity and the price of those things, so for example he orders 4 things and for that there are 4 maps in which there are the item names, the quantity and the price stored.
Now I don't know how I can retrieve this data from Firebase within Swift and save it in a dictionary. For other data types I always used a SnapshotListener, but I really have no idea how that would work for data in maps that I want to save in dictionaries.
Edit: My firebase structure would be like this and I want to save this data (item name, quantity, price) in one dictionary in Swift, so that I can pass on this information as one.
Edit 2:
class readDrinksFromDb: ObservableObject {
@Published var items = [Item]()
@ObservedObject var userInfo = UserData()
init () {
let db = Firestore.firestore()
let itemsRef = db.collection("drinks")
let loungeRef = itemsRef.document(userInfo.user.reserviert) // Should get the lounge Name -> XYZ Lounge
loungeRef.getDocument { (snap, err) in
if let err = err {
print(err.localizedDescription)
return
}
guard let snap = snap else { return }
guard let dict = snap.data() else { return }
dict.forEach { i in
let orderItems = dict[i.key] as! [String: String]
let id = orderItems["id"] ?? ""
let price = orderItems["price"] ?? ""
let pic = orderItems["pic"] ?? ""
let name = orderItems["name"] ?? ""
self.items.append(Item(id: id, name: name, price: price, pic: pic, lounge: "Rebell Lounge", type: "drinks"))
}
print(self.items)
}
}
}
