How to observe changes in UserDefaults?

后端 未结 2 2082
日久生厌
日久生厌 2020-12-21 18:53

I have an @ObservedObject in my View:

struct HomeView: View {

    @ObservedObject var station = Station()

    var body: some View {
        Tex         


        
2条回答
  •  悲哀的现实
    2020-12-21 19:30

    Here is possible solution

    import Combine
    
    // define key for observing
    extension UserDefaults {
        @objc dynamic var status: String {
            get { string(forKey: "status") ?? "OFFLINE" }
            set { setValue(newValue, forKey: "status") }
        }
    }
    
    class Station: ObservableObject {
        @Published var status: String = UserDefaults.standard.status {
            didSet {
                UserDefaults.standard.status = status
            }
        }
    
        private var cancelable: AnyCancellable?
        init() {
            cancelable = UserDefaults.standard.publisher(for: \.status)
                .sink(receiveValue: { [weak self] newValue in
                    guard let self = self else { return }
                    if newValue != self.status { // avoid cycling !!
                        self.status = newValue
                    }
                })
        }
    }
    

    Note: SwiftUI 2.0 allows you to use/observe UserDefaults in view directly via AppStorage, so if you need that status only in view, you can just use

    struct SomeView: View {
        @AppStorage("status") var status: String = "OFFLINE"
        ...
    

提交回复
热议问题