Hey everyone,
I have been working on a personal project and right now I am struggling to figure out why the user’s data will not display after the app reloads.
When the user first signs in to the app their information is displayed. However, if I close the app and then re-open it, the information is no longer there.
It seems on the reload, the data takes longer to load than the actual view itself. For test purposes, I set up a button that would print a piece of the user’s data to the console, and that worked just fine.
Any idea of how I can go about fixing this?
My code will be found below!
// Launch View
import SwiftUI
import FirebaseAuth
struct LaunchView: View {
@EnvironmentObject var model: UserModel
var body: some View {
if model.loggedIn == false {
// Show LoginView
LoginView()
.onAppear {
// Check if user is logged in or out
self.model.checkLogin()
}
}
else {
// Show ProfileView
TabView {
ProfileView()
.tabItem {
VStack {
Image(systemName: "person")
Text("Profile")
}
}
Text("Sign Out")
.tabItem {
VStack {
Image(systemName: "rectangle.portrait.and.arrow.right")
Text("Sign Out")
}
}
.onAppear {
// Sign out the user
try! Auth.auth().signOut()
// Change to logged out view
self.model.checkLogin()
}
}
.onAppear {
UITabBar.appearance().backgroundColor = UIColor(red: 248/255, green: 247/255, blue: 248/255, alpha: 100)
}
}
}
}
struct LaunchView_Previews: PreviewProvider {
static var previews: some View {
LaunchView()
}
}
// UserModel
import Foundation
import Firebase
import FirebaseAuth
class UserModel: ObservableObject {
// Authentication
@Published var loggedIn = false
// Reference to Cloud Firestore database
private let db = Firestore.firestore()
// MARK: Authentication Methods
func checkLogin() {
// Check if their is a current user to determine login status
loggedIn = Auth.auth().currentUser != nil ? true : false
// Check if user meta data has been fetched. If the user was already logged in from a previous session, we need to get their data in a seperate call
if UserService.shared.user.displayName == "" {
getUserData()
}
}
// MARK: Data Methods
func checkUsername(username: String, completion: @escaping (_ userFound: Bool) -> ()) {
// Check for the entered username
let ref = db.collection("users").whereField("username", isEqualTo: username)
ref.getDocuments { snapshot, error in
if let error = error {
print("Error finding usernames: \(error)")
}
else if snapshot!.count > 0 {
completion(true)
} else {
completion(false)
}
}
}
func getUserData() {
// Check that there's a logged in user
guard Auth.auth().currentUser != nil else {
return
}
// Get the meta data for that user
let ref = db.collection("users").document(Auth.auth().currentUser!.uid)
ref.getDocument { snapshot, error in
// Check there's no errors
guard error == nil && snapshot != nil else {
return
}
// Parse the data out and set the user meta data
let data = snapshot!.data()
let user = UserService.shared.user
user.displayName = data?["displayName"] as? String ?? ""
user.favouriteTeam = data?["favouriteTeam"] as? String ?? ""
}
}
func saveUserData(username: String, favTeam: Int) {
// Save the first name
let firebaseUser = Auth.auth().currentUser
let ref = db.collection("users").document(firebaseUser!.uid)
ref.setData(["username":username.lowercased(),
"displayName":username,
"favouriteTeam": Constants.LCSTeams[favTeam]], merge: true)
// Update the user meta data
let user = UserService.shared.user
user.displayName = username
user.favouriteTeam = Constants.LCSTeams[favTeam]
}
}