我正在开发一个Swift应用程序,我需要知道设备何时连接到互联网,我实现了以下代码来实现这一点。
import Foundation
import Network
class NetworkMonitor: ObservableObject{
static let shared = NetworkMonitor()
private let queue = DispatchQueue.global()
private let monitor: NWPathMonitor
public private(set)var isConnected :Bool=false
public private(set)var connectionType :ConnectionType = .unknown
enum ConnectionType{
case wifi
case cellular
case unknown
}
init(){
monitor = NWPathMonitor()
}
public func startMonitoring(){
monitor.pathUpdateHandler={[weak self] path in
self?.isConnected=path.status == .satisfied
self?.getConnectionType(path)
}
monitor.start(queue: queue)
}
public func stopMonitoring(){
monitor.cancel()
}
public func getConnectionType(_ path: NWPath){
if(path.usesInterfaceType(.wifi)){
connectionType = .wifi
}else if(path.usesInterfaceType(.cellular)){
connectionType = .cellular
}else{
connectionType = .unknown
}
}
}
在调用函数startMonitoring()时出现问题。一切似乎工作正常,但我得到这个错误信息在控制台
2022-12-01 13:04:37.327650+0100 Luuk[11692:1287784] [] nw_path_evaluator_set_queue Client error: set queue after starting
2022-12-01 13:04:37.328799+0100 Luuk[11692:1287784] [] nw_path_evaluator_set_queue Client error: set queue after starting, dumping backtrace:
[arm64] libnetcore-2750.120.19.0.1
0 libnetwork.dylib 0x0000000181950d4c __nw_create_backtrace_string + 192
1 libnetwork.dylib 0x0000000181166f70 nw_path_evaluator_set_queue + 304
2 libnetwork.dylib 0x00000001811841f8 nw_path_monitor_set_queue + 96
3 libswiftNetwork.dylib 0x00000001f0846ee4 $s7Network13NWPathMonitorC5start5queueySo012OS_dispatch_E0C_tF + 72
4 Luuk 0x0000000104cfa418 $s4Luuk14NetworkMonitorC15startMonitoringyyF + 216
5 Luuk 0x0000000104c7d010 $s4Luuk11SummaryViewV4bodyQrvgyycfU1_yyScMYccfU_ + 56
6 Luuk 0x0000000104c137e0 $sIeg_IeyB_TR + 48
7 libdispatch.dylib 0x0000000105640c70 _dispatch_call_block_and_release + 32
8 libdispatch.dylib 0x00000001056427c0 _dispatch_client_callout + 20
9 libdispatch.dylib 0x0000000105652c68 _dispatch_main_queue_drain + 1204
10 libdispatch.dylib 0x00000001056527a4 _dispatch_main_queue_callback_4CF + 44
11 CoreFoundation 0x00000001803fe800 5198FB57-5645-3B34-A49F-F32B52256CF3 + 333824
12 CoreFoundation 0x00000001803b8704 5198FB57-5645-3B34-A49F-F32B52256CF3 + 46852
13 CoreFoundation 0x00000001803cbbc8 CFRunLoopRunSpecific + 600
14 GraphicsServices 0x000000019c4ff374 GSEventRunModal + 164
15 UIKitCore 0x0000000182d3b648 3ED35565-456D-33CB-B554-6C567FA81585 + 5326408
16 UIKitCore 0x0000000182abcd90 UIApplicationMain + 364
17 SwiftUI 0x000000018821df24 5B6954AE-CE76-34AC-90F9-F7A8DEE0910F + 2359076
18 SwiftUI 0x000000018814be08 5B6954AE-CE76-34AC-90F9-F7A8DEE0910F + 1498632
19 SwiftUI 0x000000018812d0f4 $s7SwiftUI3AppPAAE4mainyyFZ + 128
20 Luuk 0x0000000104ce5318 $s4Luuk0A3AppV5$mainyyFZ + 40
21 Luuk 0x0000000104ce53c0 main + 12
22 dyld 0x000000010540dce4 start + 520
我在网上搜索了,但是没有很多关于这种错误的信息。如有任何帮助,不胜感激。
当我注释掉
一行时monitor.start(queue: queue)
错误消失
该错误可能是由于在取消NWPathMonitor后重复使用相同的实例而引起的-如下所述-
当NWPathMonitor实例被取消时,你不能再次使用start()。唯一的方法就是创建一个新的实例。
如果您试图取消->从同一个实例开始,您可以检查代码吗?
另外,我想说你应该改变你设置start()的队列,而不是全局的,你可以为此目的设置一个特定的
DispatchQueue(label: "NetworkMonitor")
,甚至使用主线程。