Reputation: 31
I'm new with swift and firebase. I write data on firebase by ID (field textfields on view controller), but it doesn't load on screen (on labels in another view controller). Here is my code for load user.
func loadUser(){
guard let uid = Auth.auth().currentUser?.uid else{
print("user is not logged in")
return
}
Firestore.firestore().collection("userInfo").document(uid).addSnapshotListener { (snapshot, error) in
if let snapshot = snapshot {
let user = UserProfile(data: snapshot)
self.user = user
if let firstName = user.firtsName,
let familyName = user.familyName,
let dateOfEvent = user.dateOfEvent{
self.namesLabel.text = "\(firstName)\(familyName)"
self.dateLabel.text = dateOfEvent
}else{
print("error user")
}
}
}
}
Upvotes: 1
Views: 62
Reputation: 1317
It could be that you finished loading the screen element before Firebase returned with your values. The call to firebase is async, meaning the app does not wait for Firebase to return to keep building and loading elements.
It is impossible to know if this is the cause given the part of the code you displayed. Change your code to the following to display the returned values in the command line for debuggin:
func loadUser(){
guard let uid = Auth.auth().currentUser?.uid else{
print("user is not logged in")
return
}
Firestore.firestore().collection("userInfo").document(uid).addSnapshotListener { (snapshot, error) in
if let snapshot = snapshot {
let user = UserProfile(data: snapshot)
self.user = user
if let firstName = user.firtsName,
let familyName = user.familyName,
let dateOfEvent = user.dateOfEvent {
self.namesLabel.text = "\(firstName)\(familyName)"
self.dateLabel.text = dateOfEvent
print(">> DEBUG MESSAGE from FIREBASE",
self.namesLabel.text,
"\(firstName) \(familyName)",
self.dateLabel.text,
dateOfEvent)
}else{
print("error user")
}
}
}
}
If you can see the debug message in XCode but you cannot see the text in the screen you know the problem is not in Firebase and you probably need to handle the async calls.
Upvotes: 2