Hello everyone! If someone had faced the same problem and managed to solve it, I’d be very grateful if you share your solution.
btw: I’ve already try to copy/past completed code for this lesson and still have this problem 
Expected app’s behavior after Socialcademy 9 lesson completion:
- Start my app and go to create account form
- Fill in “create account” form where I set my name to “Anton”, click “Submit”
- Move to App’s main screen with posts list which is empty now
- On the main screen I click “create a new post”
- Fill in title and content of the post, click “Submit”
- See App’s main screen with a new post, where the author’s name is “Anton”
Actual behavior:
On step 6) I don’t see author’s name (it’s just empty). This field is also empty in Firestore.
But! If I sign out and then sign in again, all newly created posts will have author’s name (while old posts’ author name field will be still empty).
I’m having the exact same problem. The author’s name isn’t displayed on the PostRow. However, I displayed the AuthViewModel’s user.name within the ProfileView and was able to display it. It seems like the AuthViewModel and the PostsViewModel (or the PostsRepository) aren’t in sync just right after you create a new user.
Now I’m really curious about how to solve this 
I’m not sure if this is actually the fix, but I was having exactly the same issue.
I rectified it by removing the “private” from the “let user: User” in the ViewModelFactory file.
Any new post I made after the fix, had the author’s name attached to the post as well as the ID.
It was dangrimm’s idea of trying to use the User’s name in the ProfileView, that led XCode to tell me I couldn’t use the user as it was private, if i used the ViewModelFactory as an environment object. (like we are in the MainTabView)
It probably has some other unknown side effect, knowing my coding skills, but then that’s what we’re all trying to learn about, right?
1 Like
I’ve been at this for DAYS… someone please solve and explain
I FIXED IT.
Solution:
In AuthView I wrapped the if let statement in an if statement that checks first if the viewModel.user is set and that their name property is not an empty string.
var body: some View {
if viewModel.user != nil && viewModel.user?.name != "" {
if let user = viewModel.user {
MainTabView()
.environmentObject(ViewModelFactory(user: user))
}
} else {
NavigationView {
SignInForm(viewModel: viewModel.makeSignInViewModel()) {
NavigationLink("Create Account", destination: CreateAccountForm(viewModel: viewModel.makeCreateAccountViewModel()))
}
}
}
}
}
Explanation:
I think everything was working fine, but the initial values to user are set to a random UID and an empty string, and that is being pushed to the AuthViewModel which is triggering the if let user statement in AuthView BEFORE the user’s name is updated. So even though everything is updating properly it’s to late because the old data is pushed through to create the PostsListViewModel.
So my fix was to simply wait for the AuthViewModel.user set to != nil AND != “”
I’m assuming this would be a problem if the user inputs an empty string as their actual name… but for now I’m moving on.
I figured this issue out, for anyone that suffered through this issue. (FYI, when you run the course’s code, the user name doesn’t appear either
)
in the createAccount function in the AuthService, i did the following (per Mengelola Pengguna di Firebase | Firebase Authentication):
let result = try await auth.createUser(withEmail: email, password: password)
let changeRequest = result.user.createProfileChangeRequest()
changeRequest.displayName = name
try await changeRequest.commitChanges()
user?.name = name
instead of the following (per Socialcademy 8)
let result = try await auth.createUser(withEmail: email, password: password)
try await result.user.updateProfile(\.displayName, to: name)
user?.name = name
You could also place the change request logic in the updateProfile function as well (personally, i thought that wasn’t necessary so I handled everything in the createAccount function).
Hope this helps those who come across this issue in the future.