Hello together,
I have a huge problem in one of my apps with the firebase authentication. I have a login/registration view and after the sign in or sign up I call a function in my ViewModel which selects additional information of the user out of the firestore database (e.g. first name, last name) and stores it in a UserModel. So after that there is a view HomeView which shows the logged in user.firstname and user.lastname of the UserModel. Therefore I wrote a UserServiceClass:
class UserService {
var user = User()
static var shared = UserService()
private init(){}
}
It all works fine after direct login or registration, but for example when I close my app after the login (also in the background) and restart it, the landing page is the HomeView (because the user is logged in anyway), but it does not show the user’s first name and lastname.
My entry point into the app is the LaunchView where the property isLoggedIn decides whether to show the login/registration view or the HomeView with the user details. The property isLoggedIn is always false, but when the login/registration View appears a function (checkLogin) in the ViewModel checks if there is a logged in user and sets the property isLoggedIn and also calls another method which gets the user information out of the database and stores it in the User Model.
So in my opinion after the restart of the app, the login/registration view appears and starts the checkLogin function. Then it checks that there is a user logged in and switches the loggedIn property to true and also calls the function for getting the user information. So because the loggedIn property is true now the app shows the HomeView but unfortunately without the user’s first name and last name. This does only work if I set a Published property after setting the document snapshot props to the UserModel props. Only then the user’s first name and last name show up. Does anyone know this problem? Is it because the home view starts to fast? Here is my getuserInformation Function:
func getUserInfoAfterLogin() {
guard Auth.auth().currentUser != nil else {
return
}
let db = Firestore.firestore()
let ref = db.collection("users").document(Auth.auth().currentUser!.uid)
ref.getDocument { result, error in
if error == nil && result != nil {
let data = result!.data()
let user = UserService.shared.user
user.vorname = data!["vorname"] as? String ?? "QVorname"
user.name = data!["name"] as? String ?? "QName"
user.image = data!["image"] as? String ?? "QImage"
self.fakePublish = ""
}
}
}
Thanks in advance.