SwiftUI:编译器从 ViewModel 获取初始化值而不是已获取的值

SwiftUI: compiler takes initialized value from ViewModel rather than the value that has been fetched

我正在编写一个程序,在其中引用一个数据库,其中每个经过身份验证的用户都有一个文档,其 ID 对应于他们的用户 ID。鉴于用户的 ID,我试图确定他们的名字;我已经设法读取了所有用户的数据,它在我的 class 用户数据模型中:

class Users {  
var id: String
var name: String
var surname: String // ...
}

在我的 ViewModel 中,我有

@Published var specificUser = User(id: "", name: "", surname: "", email: "", profficiency: 0, lists: [[]])

这是一个初始化用户。 在同一个 ViewModel 中,我有一个函数可以从数据库中获取用户数据,这似乎可以正常工作。然后它应该将新用户数据存储在 specificUserData 变量中。

 func getData() {
    let db = Firestore.firestore()
    guard let uid = auth.currentUser?.uid else { return }
    db.collection("Users").getDocuments { result, error in
        if error == nil {
            print("Current User's ID found: \(uid)")
            if let result = result {
                // iterate through documents until correct ID is found
                for d in result.documents {
                    if d.documentID == uid {
                        print("Document ID found: \(d.documentID)")
                        self.specificUser = User(
                            id: d.documentID,
                            name: d["name"] as? String ?? "",
                            // ...
                        )
                        print(self.specificUser)
                        print(self.specificUser.name) // This works; my compiler spits out the correct name from the database, so clearly the specificUser variable has been changed. 
                    }
                }
            }
        } else {
            // Handle Error
            print("Error while fetching user's specific data")
        }
    }
}

下面是我如何初始化 getData() 函数:

init() {
    model.getData()
    print("Data Retrieval Complete")
    print("User's Name: \(model.specificUser.name)")
    
}

我正在尝试像这样引用我的 ViewModel:

@ObservedObject var model = ViewModel()

现在的问题是:当我尝试使用

从我的结构中的视图模型引用用户名时
model.specificUser.name

它给了我默认名称,即使我已经初始化了 getData() 函数。检查我的编译器日志并添加一堆 print 语句,似乎初始化实际上在工作,但它在打印正确的名称之前打印 data retrieval complete

有什么想法吗?似乎初始化函数正在从我的 ViewModel 中获取初始化值,而不是它应该计算的正确值。

试试这个

 func getData(_ completion: @escaping (Bool, User?) -> ()) {
    let db = Firestore.firestore()
    guard let uid = auth.currentUser?.uid else { return }
    db.collection("Users").getDocuments { result, error in
        if error == nil {
            print("Current User's ID found: \(uid)")
            if let result = result {
                // iterate through documents until correct ID is found
                for d in result.documents {
                    if d.documentID == uid {
                        print("Document ID found: \(d.documentID)")
                        let user = User(
                            id: d.documentID,
                            name: d["name"] as? String ?? "",
                            // ...
                        )
                        completion(true, user)
                        print(self.specificUser)
                        print(self.specificUser.name) // This works; my compiler spits out the correct name from the database, so clearly the specificUser variable has been changed. 
                    }
                }
            }
        } else {
            // Handle Error
            completion(false, nil)
            print("Error while fetching user's specific data")
        }
    }
}

init() {
    model.getData() { res, user in 
    if res { 
       self.specificUser = user! 
    }
    print("Data Retrieval Complete")
    print("User's Name: \(model.specificUser.name)")
}
    
}