SwiftUI - 从 Swift class 发起的 Observable 对象不会更新 ContentView() 上的 @ObservedObject

SwiftUI - Observable Object initiated from Swift class does not update @ObservedObject on ContentView()

ObservableObject class 正在从 ContentView() 和另一个 Swift class 实例化。当 ObservableObject class 的函数被 Swift class 运行 时,它不会更新 ContentView() 的 @ObservedObject。

我知道这是因为我实例化了 ObservableObject class 两次。当 Observable Class 被 ContentView().

实例化时,使用 @ObservedObject 的最佳实践是什么?

我还没有找到让@EnvironmentObject 与 Swift classes 一起工作的方法。
我可以使用一个全局变量和 运行 一个 Timer() 来检查对它的更改。然而,这感觉是一种丑陋的做法?!?

请参阅下面的示例代码。请运行在设备上查看打印语句。


import SwiftUI

struct ContentView: View {
    
    @ObservedObject var observedClass: ObservedClass = ObservedClass()
    
    // The callingObservedClass does not exist on the ContentView, but is called
    // somewhere in the app with no reference to the ContentView.
    // It is included here to better showcase the issue.
   
    let callingObservedClass: CallingObservedClass = CallingObservedClass()
    
    var body: some View {
        
        VStack {
   
            // This Text shall be updated, when
            // self.callingObservedClass.increaseObservedClassCount() has been executed.
   
            Text(String(observedClass.count))
            
            Button(action: {
    
                // This updates the count-variable, but as callingObservedClass creates
                // a new instance of ObservedClass, the Text(observedClass.count) is not updated.
   
                self.callingObservedClass.increaseObservedClassCount()
            }, label: {
                Text("Increase")
            })
        }
    }
}


class CallingObservedClass {
    
    let observedClass = ObservedClass()
    
    func increaseObservedClassCount() {
   
        // Returning an Int here to better showcase that count is increased.
        // But not in the ObservedClass instance of the ContentView, as the
        // Text(observedClass.count) remains at 0.
    
        let printCount = observedClass.increaseCount()
        print(printCount)
    }
}


class ObservedClass: ObservableObject {
    @Published var count: Int = 0
    
    func increaseCount() -> Int {
        count = count + 1
        return count
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

编辑:我想我的问题是如何从 Swift class 获取数据并在数据发生变化时更新 SwiftUI 视图无法从 SwiftUI 视图实例化 Swift class。

一个可能的解决方案是链接 ObservableObject 类。不幸的是,从 iOS 13.6 开始,这不是开箱即用的。

我通过以下方式找到了答案:

调整后的功能示例:

// Add Combine

import Combine
import SwiftUI

struct ContentView: View {

    @ObservedObject var callingObservedClass: CallingObservedClass = CallingObservedClass()
    
    var body: some View {
        
        VStack {

            // Calling the chained ObservableObject
            
            Text(String(callingObservedClass.observedClass.count))
            
            Button(action: {

                self.callingObservedClass.increaseObservedClassCount()
            }, label: {
                Text("Increase")
            })
        }
    }
}

class CallingObservedClass: ObservableObject {
    
    // Chaining Observable Objects
    
    @Published var observedClass = ObservedClass()
    
    // ObservableObject-chaining does not work out of the box.
    // The anyCancellable variable with the below init() will do the trick.
    // Thanks to 
    
    var anyCancellable: AnyCancellable? = nil
    
    init() {
        anyCancellable = observedClass.objectWillChange.sink { (_) in
            self.objectWillChange.send()
        }
    }
    
    func increaseObservedClassCount() {
        let printCount = observedClass.increaseCount()
        print(printCount)
    }
}

class ObservedClass: ObservableObject {
    @Published var count: Int = 0
    
    func increaseCount() -> Int {
        count = count + 1
        return count
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

如果 ObservableObject-Chaining 不是一个选项,我对如何访问 Swift Class 数据和更新 SwiftUI 视图很感兴趣。

如果您对此有解决方案,请在下面回答。