I got a class with a function that gets the username for printing it on the view. It works, but I got a delay of nearly a second where the username is not seen.
here the class :
class ViewModel: ObservableObject {
@Published var username: String = ""
func benutzerdaten_accountview (email: String) -> String {
let db = Firestore.firestore()
let docRef = db.collection("Nutzer").document(email)
docRef.getDocument{ (document, error) in
if let document = document, document.exists{
self.username = (document.data()!["username"] as? String)!
}
}
return username
}
}
This is how I print it on the view :
struct AccountView: View{
@ObservedObject var model = ViewModel()
@Binding var tabSelection: Int
var email: String = (Auth.auth().currentUser?.email)!
var body: some View {
VStack{
Text("Hallo, \(model.benutzerdaten_accountview(email: email).description)")
...
usernameis actually returned. By the looks of it, I don't think it would change from the original empty string, until after. So the return is pointless for a value set in an asynchronous closure.