Firebase - Additional User Information is not stored

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.

Are you saving the data to a database? Just fetching the data from Firebase is great to get the data, but if you want it to be persisted across app sessions (after closing and opening again)

You’d have to fetch it again, or save the data locally to a database and then fetch from the database on load

I save it to the database when the user signs up. I fetch it from the database when the user signs in and I have it to the UserModel. Then I have the function checkLogin on AppStart and this function fetches the data and saves it to the UserModel again when I open the App after closing and not logging out. I tried to do it similar to Chris with his learning app. There he also fetches the user name in his checkLogin function on App Start, but in my code it only works if I set kind of a “fake Published” property after I set the user data to the User Model :frowning:

I think my problem is that my View initializes before the data is saved to the User Model. Is there something I can do to wait until the data is set?