我有一个网络监视器,我想在状态发生变化时从那里接收通知。
看起来像这样:
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()
}