[ad_1]
Principally, I’ve a category which fetches profile data from Firestore, however once I edit the profile and submit the modifications and return to the profile view, the data is not up to date, however it’s up to date inside Firestone, so I’ve to shut the app and relaunch it for it to replace throughout the app. I am assuming my concern is as a result of I’m not recalling the category or the view is not being up to date? Right here is my code for the profile view, and the category I name to fetch the info from Firestore.
ProfileView.swift
import SwiftUI
import FirebaseAuth
import FirebaseFirestore
struct ProfileView: View {
let auth = Auth.auth()
@ObservedObject personal var person = MainUserDataModel() //pulls person knowledge from firebase (positioned in fetchUserData.swift) works like: person.currentUser?.uid ?? ""
@State personal var firstName = ""
@State personal var lastName = ""
@State personal var e mail = ""
var physique: some View {
VStack {
Picture("mank")
.resizable()
.body(width: 100, top: 100)
.clipShape(Circle())
.padding()
Textual content("(firstName) (lastName)")
.fontWeight(.daring)
Textual content("(e mail)")
.font(.caption)
.padding(5)
NavigationLink("Edit Profile", vacation spot: EditProfile())
.foregroundColor(Coloration.pink)
Type {
}
}
.job {
firstName = person.currentUser?.firstName ?? ""
lastName = person.currentUser?.lastName ?? ""
e mail = person.currentUser?.e mail ?? ""
}
//.navigationTitle("Profile")
}
}
struct ProfileView_Previews: PreviewProvider {
static var previews: some View {
ProfileView()
}
}
fetchUserData.swift
import Basis
import FirebaseAuth
import Firebase
import FirebaseFirestore
struct Consumer {
let firstName, lastName, uid, e mail: String
}
class MainUserDataModel: ObservableObject {
@Revealed var errorMessage = ""
@Revealed var currentUser: Consumer?
init() {
fetchUserData()
}
personal func fetchUserData() {
let auth = Auth.auth()
guard let uid = auth.currentUser?.uid else { return }
Firestore.firestore().assortment("customers").doc(uid).getDocument { snapshot, error in
if let error = error {
self.errorMessage = "Didn't fetch present person: (error)"
print("Didn't fetch present person:", error)
return
}
guard let knowledge = snapshot?.knowledge() else {
self.errorMessage = "No knowledge discovered"
return
}
let firstName = knowledge["firstName"] as? String ?? ""
let lastName = knowledge["lastName"] as? String ?? ""
let uid = knowledge["uid"] as? String ?? ""
let e mail = knowledge["email"] as? String ?? ""
self.currentUser = Consumer(firstName: firstName, lastName: lastName, uid: uid, e mail: e mail)
}
}
}
[ad_2]
