簡體   English   中英

Swiftui 強制更新查看

[英]Swiftui Force Update View

在我的內容視圖中,我有一個主頁,其中包含一些文本,上面寫着“歡迎,xxxx”,其中 xxxx 是從 firebase 數據庫中獲取的名稱。 可以在通過導航鏈接導航到的設置頁面中更改此字段。 當更改名稱並保存主頁上的名稱時,僅當您強制關閉應用程序時才會更新。 當您從設置中按下后退按鈕時,如何強制更新視圖。

這就是我顯示該字段的方式:

Text("Welcome, \(companyName)")
                        .font(.system(size: 23))
                        .bold()
                        .foregroundColor(Color("background"))
                        .padding(.bottom, 50)

這就是我為 companyName 設置值的方式:

func SetData() {
    
    var db = Firestore.firestore()
    let user = Auth.auth().currentUser
    let userName = user?.email ?? ""
    let docRef = db.collection("CONTACT").document(userName)
    
    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            
            //Setting Values
            let data = document.data()
            self.companyName = data?["companyName"] as? String ?? ""
            
        } else {
            print("Document does not exist")
        }
    }
    
}

有幾種解決方案,但您沒有提供足夠的代碼來概述您為修改變量companyName所做的工作。 最簡單的解決方案是將 companyName 作為綁定值傳遞到設置中。

我在這里想象的是您的 HomeView 正在獲取啟動時的數據。 在設置中,發出了更改數據請求,但沒有對 HomeView 中的數據進行任何更新。 通過使用綁定變量,我們可以確保 companyName 連接到 HomeView 中的真實來源,因此 function 修改了 companyName,它恰好是 HomeView 上的公司名稱,而不是潛在地修改 companyName 的值。

struct HomeView: View {
    @State var companyName = "Microsoft"
    
    var body: some View {
        NavigationView {
            NavigationLink(destination: SettingsView(companyName: $companyName)) {
                Text("Tap to navigate to Settings")
            }
        }
    }
}

struct SettingsView: View {

@Binding var companyName : String

    var body: some View {
        Button {
            SetData()
        } label: {
            HStack {
                Text("Tap to change!")
                Text("\(companyName)!")
            }
        }
    }
    
    
    func SetData() {
        
        var db = Firestore.firestore()
        let user = Auth.auth().currentUser
        let userName = user?.email ?? ""
        let docRef = db.collection("CONTACT").document(userName)
        
        docRef.getDocument { (document, error) in
            if let document = document, document.exists {
                
                //Setting Values
                let data = document.data()
                self.companyName = data?["companyName"] as? String ?? ""
                
            } else {
                print("Document does not exist")
            }
        }
    }
}

如果您已經這樣做了,它不會以某種方式起作用,另一種解決方案是在您的 HomeView 中添加一個.onAppear修飾符。

struct HomeView: View {
    @State var companyName = "Microsoft"

    var body: some View {
        VStack {
            // code ...
        }
        .onAppear {
            fetchData()
        }

    }

    func fetchData() {
        // code that returns companyFetchedName
        self.companyName = companyFetchedName
    }
}

在主隊列上修改它,比如

docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        
        //Setting Values
        let data = document.data()
        DispatchQueue.main.async {    // << here !!
          self.companyName = data?["companyName"] as? String ?? ""
        }
        
    } else {
        print("Document does not exist")
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM