0

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

 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)
    }
}

我在 Home 中创建了一个网络监视器的@StateObject并通过环境对象发送它。

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()
}
4

1 回答 1

4

您在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)
        }
     }
 }
于 2021-06-22T19:35:10.667 回答