【问题标题】:Updating Text Using SwiftUI and FireStore使用 SwiftUI 和 FireStore 更新文本
【发布时间】:2020-10-01 17:37:57
【问题描述】:

我在我的应用程序中存储了一个名为 month hours 的值,用于跟踪用户使用应用程序的小时数并将其显示在一行文本中。如果文本是 Swift UI 中堆栈的一部分,但我无法弄清楚如何在查询信息后更新文本我已经尝试了很多方法来使这项工作从结构到类到使用 @状态。

这只是我尝试过的最新方法,但如果有人可以提供帮助,将不胜感激。

let db = Firestore.firestore()


class Month {
    var monthHours = "0"

    func getMonthHours() {
        db.addSnapshotListener(.  //Im removing the actual query part to keep that private but the print statement below confirms the query is not the issue. 
            { (docSnapShot, err) in
                if let e = err {
                    print("There was an error retrieving the monthly hours:\n\(e.localizedDescription)")
                } else {
                    let data = docSnapShot?.data()
                    if let h = data?[K.FStore.monthHoursField] as? Double {
                        self.monthHours = String(h.rounded())
                        print("These are the hours:\n\(self.monthHours)")
                    }
                }
        })
    }
    func getMonth() -> String {
            let date = Date()
            let formatter = DateFormatter()
            formatter.dateFormat = "MMMM yyyy"
            let result = formatter.string(from: date)
            return result
    }
    init() {
        getMonthHours()
    }
}

struct ChartView : View {
    @State private var month = Month()
    //Struct variables
    var body  : some View {
        ZStack {
            Color(UIColor(named: K.BrandColors.grey)!).edgesIgnoringSafeArea(.all)
            VStack {
                Text("HOURS THIS MONTH \(month.monthHours)")
                    .font(.system(size: 18))
                    .fontWeight(.heavy)
            }
        } 
} 

【问题讨论】:

    标签: swift google-cloud-firestore swiftui updates


    【解决方案1】:

    这概述了一种可能的方法。关键是处理异步函数“getMonthHours”。您需要等到它完成提取后才能使用结果。

    class Month {
        var monthHours = "0"
    
        // async fetch the month hours from Firestore, ... error handling todo
        static func getMonthHours(handler: @escaping (String) -> Void) {
            db.addSnapshotListener{ (docSnapShot, err) in
                if let e = err {
                    print("There was an error retrieving the monthly hours:\n\(e.localizedDescription)")
                    return handler("") // should return some error here .... todo
                } else {
                    if let data = docSnapShot?.data(),
                        let h = data?[K.FStore.monthHoursField] as? Double {
                        // return the result
                        return handler(String(h.rounded()))
                    } else {
                        return handler("") // should return some error here .... todo
                    }
                }
            }
        }
    
        func getMonth() -> String {
            let date = Date()
            let formatter = DateFormatter()
            formatter.dateFormat = "MMMM yyyy"
            let result = formatter.string(from: date)
            return result
        }
    
        init() { }
    }
    
    struct ChartView : View {
    @State private var monthHours = ""
    var body  : some View {
        ZStack {
            Color(UIColor(named: K.BrandColors.grey)!).edgesIgnoringSafeArea(.all)
            VStack {
                Text("HOURS THIS MONTH \(monthHours)")
                    .font(.system(size: 18))
                    .fontWeight(.heavy)
            }
        }.onAppear(perform: loadData)
        }
    }
    
    func loadData() {
        // when the fetching is done it will update the view
        Month.getMonthHours() { hours in
            self.monthHours = hours
        }
    }
    

    【讨论】:

    • 非常感谢你帮了很多忙,你不知道我调试了多久哈哈!非常感谢!
    猜你喜欢
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多