基本上,我有一個從 Firestore 獲取組態檔資訊的類,但是當我編輯組態檔并提交更改并回傳組態檔視圖時,資訊沒有更新,但它在 Firestone 中更新,所以我必須關閉應用程式并重新啟動它以使其在應用程式內更新。我假設我的問題是因為我不記得課程或視圖沒有更新?這是我的組態檔視圖代碼,以及我為從 Firestore 獲取資料而呼叫的類。
ProfileView.swift
import SwiftUI
import FirebaseAuth
import FirebaseFirestore
struct ProfileView: View {
let auth = Auth.auth()
@ObservedObject private var user = MainUserDataModel() //pulls user data from firebase (located in fetchUserData.swift) works like: user.currentUser?.uid ?? ""
@State private var firstName = ""
@State private var lastName = ""
@State private var email = ""
var body: some View {
VStack {
Image("mank")
.resizable()
.frame(width: 100, height: 100)
.clipShape(Circle())
.padding()
Text("\(firstName) \(lastName)")
.fontWeight(.bold)
Text("\(email)")
.font(.caption)
.padding(5)
NavigationLink("Edit Profile", destination: EditProfile())
.foregroundColor(Color.pink)
Form {
}
}
.task {
firstName = user.currentUser?.firstName ?? ""
lastName = user.currentUser?.lastName ?? ""
email = user.currentUser?.email ?? ""
}
//.navigationTitle("Profile")
}
}
struct ProfileView_Previews: PreviewProvider {
static var previews: some View {
ProfileView()
}
}
fetchUserData.swift
import Foundation
import FirebaseAuth
import Firebase
import FirebaseFirestore
struct User {
let firstName, lastName, uid, email: String
}
class MainUserDataModel: ObservableObject {
@Published var errorMessage = ""
@Published var currentUser: User?
init() {
fetchUserData()
}
private func fetchUserData() {
let auth = Auth.auth()
guard let uid = auth.currentUser?.uid else { return }
Firestore.firestore().collection("users").document(uid).getDocument { snapshot, error in
if let error = error {
self.errorMessage = "Failed to fetch current user: \(error)"
print("Failed to fetch current user:", error)
return
}
guard let data = snapshot?.data() else {
self.errorMessage = "No data found"
return
}
let firstName = data["firstName"] as? String ?? ""
let lastName = data["lastName"] as? String ?? ""
let uid = data["uid"] as? String ?? ""
let email = data["email"] as? String ?? ""
self.currentUser = User(firstName: firstName, lastName: lastName, uid: uid, email: email)
}
}
}
uj5u.com熱心網友回復:
您在完成@State之前設定變數fetchUserData。@State完全洗掉變數并僅依賴于@Published您的值會更干凈MainUserDataModel:
struct ProfileView: View {
let auth = Auth.auth()
@ObservedObject private var user = MainUserDataModel()
var body: some View {
VStack {
Image("mank")
.resizable()
.frame(width: 100, height: 100)
.clipShape(Circle())
.padding()
if let currentUser = user.currentUser {
Text("\(currentUser.firstName) \(currentUser.lastName)")
.fontWeight(.bold)
Text("\(currentUser.email)")
.font(.caption)
.padding(5)
NavigationLink("Edit Profile", destination: EditProfile())
.foregroundColor(Color.pink)
} else {
//Display a loading indicator
}
}
}
}
您還可以使用之前提供默認值的策略:
struct ProfileView: View {
let auth = Auth.auth()
@ObservedObject private var user = MainUserDataModel()
var body: some View {
VStack {
Image("mank")
.resizable()
.frame(width: 100, height: 100)
.clipShape(Circle())
.padding()
Text("\(user.currentUser?.firstName ?? "") \(user.currentUser?.lastName ?? "")")
.fontWeight(.bold)
Text("\(user.currentUser?.email ?? "")")
.font(.caption)
.padding(5)
NavigationLink("Edit Profile", destination: EditProfile())
.foregroundColor(Color.pink)
}
}
}
相反,如果您確實需要變數,則在加載資料@State之前不應設定它們——您可以使用并觀察發布者的更改。MainUserDataModelonReceivecurrentUser
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/427948.html
