View refreshing not triggered when ObservableObject is inherited in SwiftUI

后端 未结 3 505
慢半拍i
慢半拍i 2020-12-21 00:44

ContentView2 view is not refreshed when model.value changes, if Model conforms to ObservableObject directly instead of in

相关标签:
3条回答
  • 2020-12-21 01:05

    Here is working variant of your example. See that to be able to work, not only chaining the publishers is required, but at least one Published property. So or so, it could help in some scenario.

    import SwiftUI
    
    class SuperModel: ObservableObject {
        // this is workaround but not real trouble.
        // without any value in supermodel there is no real usage of SuperModel at all
        @Published var superFlag = false
    }
    
    class Model: SuperModel {
        @Published var value = ""
        override init() {
            super.init()
            _ = self.objectWillChange.append(super.objectWillChange)
        }
    }
    
    struct ContentView: View {
    
        @ObservedObject var model = Model()
    
        var body: some View {
            VStack {
                Text(model.value)
                Button("change value") {
                    self.model.value = "\(Int.random(in: 1...10))"
                }
            }
    
        }
    }
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

    changing the code to

    var body: some View {
            VStack {
                Text(model.value)
                Button("change value") {
                    self.model.value = "\(Int.random(in: 1...10))"
                }
                Text(model.superFlag.description)
                Button("change super flag") {
                    self.model.superFlag.toggle()
                }
            }
    
        }
    

    you can see how to use even your supermodel at the same time

    0 讨论(0)
  • 2020-12-21 01:11

    This really looks like heavy defect.

    class SuperModel: ObservableObject {
    }
    
    class Model: SuperModel {
        @Published var value = ""
    }
    

    as I see the value is changed and keep new one as expected, but DynamicProperty feature does not work

    The following variant works for me (Xcode 11.2 / iOS 13.2)

    class SuperModel: ObservableObject {
        @Published private var stub = "" // << required !!!
    }
    
    class Model: SuperModel {
        @Published var value = "" {
            willSet { self.objectWillChange.send() } // < works only if above
        }
    }
    

    Also such case is possible for consideration:

    class SuperModel {
    }
    
    class Model: SuperModel, ObservableObject {
        @Published var value = ""
    }
    
    0 讨论(0)
  • 2020-12-21 01:15

    Use ObjectWillChange to solve the problem specified.

    Here is the working code:

    import SwiftUI
    
    class SuperModel: ObservableObject {
    
    }
    
    class Model: SuperModel {
        var value: String = "" {
            willSet { self.objectWillChange.send() }
        }
    }
    
    struct ContentView: View {
    
        @ObservedObject var model = Model()
    
        var body: some View {
            VStack {
                Text("Model Value1: \(model.value)")
                Button("change value") {
                    self.model.value = "\(Int.random(in: 1...10))"
                }
                Text("Model Value2: \(model.value)")
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题