问题
I have a class, called UserData, which is set as an EnvironmentObject that collects the current user's data from Firestore and saves it in a public var. Now, to order, I want to load the items which each merchant has to offer, but for that I have to retrieve which merchant my user chose from the Firestore database.
I called the UserData class in my readDrinksFromDb class, but in my init() function, where I load the merchant's items, the UserData just does not show any value and is just empty, I can't retrieve any of its information, and when I print it the console just prints a newline.
How do I get the UserData to work in another class? Because as it seems, the init class comes before it even initialises the variables above.
It also gives me an error when I try to call that class: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but drinks has 1' because the UserData is just empty and could not be retrieved, therefore it points to no document at all but I am trying to retrieve the data from that document.
class UserData: ObservableObject {
static let shared = UserData()
@Published var user = User(id: "", name: "", email: "", account: "", reserviert: "", customerID: "")
init () {
let db = Firestore.firestore()
guard let userID = Auth.auth().currentUser?.uid else {
print("No user")
return
}
let userData = db.collection("users").document(String(userID))
userData.addSnapshotListener(includeMetadataChanges: true) { (snap, err) in
guard let document = snap else {
print("Document does not exist")
return
}
guard let data = document.data() else {
print("Document was empty")
return
}
self.user = User(id: data["uid"] as! String, name: data["Name"] as! String, email: data["E-Mail"] as! String, account: data["account"] as! String, reserviert: data["reserviert"] as! String, customerID: data["customerID"] as! String)
}
}
func update() {
let db = Firestore.firestore()
guard let userID = Auth.auth().currentUser?.uid else {
print("No user")
return
}
let userData = db.collection("users").document(String(userID))
userData.addSnapshotListener(includeMetadataChanges: true) { (snap, err) in
guard let document = snap else {
print("Document does not exist")
return
}
guard let data = document.data() else {
print("Document was empty")
return
}
self.user = User(id: data["uid"] as! String, name: data["Name"] as! String, email: data["E-Mail"] as! String, account: data["account"] as! String, reserviert: data["reserviert"] as! String, customerID: data["customerID"] as! String)
}
}
}
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("XYZ Lounge")
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: "XYZ Lounge", type: "drinks"))
}
print(self.items)
}
}
}
来源:https://stackoverflow.com/questions/62012986/use-firebase-class-in-init-of-other-class