小编log*_*ito的帖子

将 NWPathMonitor 与 Swift 现代并发 (AsyncStream) 与 GCD (DispatchQueue) 结合使用

我注意到start(queue:)中的方法NWPathMonitor需要类型为 的队列DispatchQueue。有没有办法使用 Swift 现代并发来实现这一点,可能使用AsyncStream

使用Apple文档AsyncStream,我已经创建了扩展NWPathMonitor,但我无法启动NWPathMonitor显示器,任何建议将不胜感激,谢谢

extension NWPathMonitor {
  static var nwpath: AsyncStream<NWPath> {
    AsyncStream { continuation in
      let monitor = NWPathMonitor()
      monitor.pathUpdateHandler = { path in
        continuation.yield(path)
      }
      continuation.onTermination = { @Sendable _ in
        monitor.cancel()
      }
      // monitor.start(queue: )
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

阅读苹果的文档

concurrency swift nwpathmonitor asyncstream

3
推荐指数
1
解决办法
712
查看次数

标签 统计

asyncstream ×1

concurrency ×1

nwpathmonitor ×1

swift ×1