Skip to content

Instantly share code, notes, and snippets.

@DavidBemerguy
Created June 7, 2023 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBemerguy/dad42579abde390c47730b5a98f08122 to your computer and use it in GitHub Desktop.
Save DavidBemerguy/dad42579abde390c47730b5a98f08122 to your computer and use it in GitHub Desktop.
Monitors network
import Network
class PathMonitor: ObservableObject {
@Published private(set) var status: NWPath.Status
private let pathMonitor = NWPathMonitor()
private let pathMonitorQueue = DispatchQueue(label: "NWPathMonitor")
init(status: NWPath.Status = .unsatisfied, active: Bool = true) {
self.status = status
if active {
enablePathMonitor()
}
}
private func enablePathMonitor() {
pathMonitor.pathUpdateHandler = { path in
DispatchQueue.main.async {
self.status = path.status
}
}
pathMonitor.start(queue: pathMonitorQueue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment