SwiftUI:环境对象发布者不在 onReceive 中发送更新

SwiftUI: Environment Object Publisher doesn't send updates in onReceive

我有一个网络监视器,我希望在状态发生变化时从中接收通知。
看起来像这样:

 final class NetworkMonitor: ObservableObject {
    let monitor = NWPathMonitor()
    let queue = DispatchQueue(label: "Monitor")

    static let shared = NetworkMonitor()

    @Published var status: NetworkStatus = .connected

    func start() {
        self.monitor.pathUpdateHandler = { [weak self] path in
            guard let self = self else { return }
            DispatchQueue.main.async {
                self.status = (path.status == .satisfied) ? .connected : .disconnected
            }
        }

        self.monitor.start(queue: self.queue)
    }
}

我在首页创建了一个@StateObject的Network monitor并通过环境对象发送。

struct HomeView: View {
    @StateObject var networkMonitor = NetworkMonitor()

    var body: some View {
        NavigationView {
            ContentView().environmentObject(networkMonitor)
        }
     }
 }

并且我希望在 ContentView 中接收发生的任何更改。

struct ContentView: View {
  @EnvironmentObject var networkMonitor: NetworkMonitor
  var body: some View {
     VStack {
         Text("Example")
     }.onReceive(self.networkMonitor.$status, perform: { status in
            print("onReceive \(status)") // <---- this doesn't trigger
       })
   }
}

我不明白为什么每当网络状态发生变化时,onReceive 都不会触发。

编辑:我正在使用共享实例在 AppDelegate 中开始监控。

     func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        NetworkMonitor.shared.start()
}

您在 AppDelegate (shared) 中使用的实例与在 HomeView (NetworkMonitor()) 中使用的实例不同。

通常,当你使用单例模式时,你希望将 init 设为私有,这样你就可以避免这个错误:

final class NetworkMonitor: ObservableObject {
    let monitor = NWPathMonitor()
    let queue = DispatchQueue(label: "Monitor")

    static let shared = NetworkMonitor()

    @Published var status: NetworkStatus = .connected

    private init() { } //<-- Here

    func start() {
        self.monitor.pathUpdateHandler = { [weak self] path in
            guard let self = self else { return }
            DispatchQueue.main.async {
                self.status = (path.status == .satisfied) ? .connected : .disconnected
            }
        }

        self.monitor.start(queue: self.queue)
    }
}
struct HomeView: View {
    @StateObject var networkMonitor = NetworkMonitor.shared //<-- Here

    var body: some View {
        NavigationView {
            ContentView().environmentObject(networkMonitor)
        }
     }
 }