gpt4 book ai didi

swift - 由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因 : 'Failed to set posix_spawn_file_actions for fd -1 at index 0

转载 作者:行者123 更新时间:2023-11-30 10:51:37 25 4
gpt4 key购买 nike

在 Swift 4 中,在 Xcode 版本 9.3.1 (9E501) 上,我利用 DispatchQueue.global(qos: .background).async {...} 运行 while (true) 在后台循环:

func appStartDispatchQueue() {
DispatchQueue.global(qos: .background).async {

while(true) {
let networkStatus = Verification.onCompanyNetwork()

DispatchQueue.main.async {
self.onNetwork = networkStatus
}
sleep(3)
}
}
}

Verification.onCompanyNetwork() 正在通过 bash 脚本运行 ping,以查看公司网络上的服务器是否有响应。

我需要这个线程始终在后台运行这个应用程序 - 所以即使它失败,我如何捕获异常并确保线程再次启动?现在我什至不确定为什么线程失败......

到目前为止发现的两个崩溃场景:

  • 运行应用程序时(在 Xcode 之外),应用程序平均在连续运行 3-6 小时后崩溃
  • 运行应用程序时(在 Xcode 内部),即使连续运行 3-6 小时,应用程序也不会崩溃

完整的错误消息如下:

Crashed Thread:        1  Dispatch queue: com.apple.root.background-qos

Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY

Application Specific Information:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9'
terminating with uncaught exception of type NSException
abort() called

Application Specific Backtrace 1:
0 CoreFoundation 0x00007fff339d9ecd __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff5fa91720 objc_exception_throw + 48
2 CoreFoundation 0x00007fff339d9cff +[NSException raise:format:] + 201
3 Foundation 0x00007fff35ccf581 -[NSConcreteTask launchWithDictionary:error:] + 3236
4 AdminAccess 0x000000010f268b32 _T011AdminAccess9ScriptingC12shellCommandSSSgSS0D4Path_SaySSG9argumentstFZ + 370
5 AdminAccess 0x000000010f266e20 _T011AdminAccess9ScriptingC13runBashScriptS2S10scriptName_SS3argtFZ + 32496
6 AdminAccess 0x000000010f25779b _T011AdminAccess12VerificationC16onCompanyNetworkSbyFZ + 107
7 AdminAccess 0x000000010f26a834 _T011AdminAccess11AppDelegateC21appStartDispatchQueueyyFyycfU_ + 36
8 AdminAccess 0x000000010f26a99d _T011AdminAccess11AppDelegateC21appStartDispatchQueueyyFyycfU_TA + 13
9 AdminAccess 0x000000010f26aa1d _T0Ieg_IeyB_TR + 45
10 libdispatch.dylib 0x00007fff60b10d53 _dispatch_call_block_and_release + 12
11 libdispatch.dylib 0x00007fff60b11dcf _dispatch_client_callout + 8
12 libdispatch.dylib 0x00007fff60b1fe80 _dispatch_root_queue_drain + 671
13 libdispatch.dylib 0x00007fff60b204bd _dispatch_worker_thread2 + 90
14 libsystem_pthread.dylib 0x00007fff60d526dd _pthread_wqthread + 619
15 libsystem_pthread.dylib 0x00007fff60d52405 start_wqthread + 13

Thread 0:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00007fff60c9917a mach_msg_trap + 10
1 libsystem_kernel.dylib 0x00007fff60c996d0 mach_msg + 60
2 com.apple.CoreFoundation 0x00007fff339230c2 __CFRunLoopServiceMachPort + 337
3 com.apple.CoreFoundation 0x00007fff33922611 __CFRunLoopRun + 1654
4 com.apple.CoreFoundation 0x00007fff33921d48 CFRunLoopRunSpecific + 463
5 com.apple.HIToolbox 0x00007fff32bb8ab5 RunCurrentEventLoopInMode + 293
6 com.apple.HIToolbox 0x00007fff32bb87eb ReceiveNextEventCommon + 618
7 com.apple.HIToolbox 0x00007fff32bb8568 _BlockUntilNextEventMatchingListInModeWithFilter + 64
8 com.apple.AppKit 0x00007fff30e73363 _DPSNextEvent + 997
9 com.apple.AppKit 0x00007fff30e72102 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1362
10 com.apple.AppKit 0x00007fff30e6c165 -[NSApplication run] + 699
11 com.apple.AppKit 0x00007fff30e5b8a3 NSApplicationMain + 780
12 mace.AdminAccess 0x000000010f26be6d main + 13 (AppDelegate.swift:18)
13 libdyld.dylib 0x00007fff60b5fed9 start + 1

Thread 1 Crashed:: Dispatch queue: com.apple.root.background-qos
0 libsystem_kernel.dylib 0x00007fff60c9f23e __pthread_kill + 10
1 libsystem_pthread.dylib 0x00007fff60d55c1c pthread_kill + 285
2 libsystem_c.dylib 0x00007fff60c081c9 abort + 127
3 libc++abi.dylib 0x00007fff5e289231 abort_message + 231
4 libc++abi.dylib 0x00007fff5e2893cd default_terminate_handler() + 265
5 libobjc.A.dylib 0x00007fff5fa93c87 _objc_terminate() + 97
6 libc++abi.dylib 0x00007fff5e294dfe std::__terminate(void (*)()) + 8
7 libc++abi.dylib 0x00007fff5e294e73 std::terminate() + 51
8 libdispatch.dylib 0x00007fff60b11de3 _dispatch_client_callout + 28
9 libdispatch.dylib 0x00007fff60b1fe80 _dispatch_root_queue_drain + 671
10 libdispatch.dylib 0x00007fff60b204bd _dispatch_worker_thread2 + 90
11 libsystem_pthread.dylib 0x00007fff60d526dd _pthread_wqthread + 619
12 libsystem_pthread.dylib 0x00007fff60d52405 start_wqthread + 13

Thread 2:: com.apple.NSEventThread
0 libsystem_kernel.dylib 0x00007fff60c9917a mach_msg_trap + 10
1 libsystem_kernel.dylib 0x00007fff60c996d0 mach_msg + 60
2 com.apple.CoreFoundation 0x00007fff339230c2 __CFRunLoopServiceMachPort + 337
3 com.apple.CoreFoundation 0x00007fff33922611 __CFRunLoopRun + 1654
4 com.apple.CoreFoundation 0x00007fff33921d48 CFRunLoopRunSpecific + 463
5 com.apple.AppKit 0x00007fff30e7bf89 _NSEventThread + 160
6 libsystem_pthread.dylib 0x00007fff60d53305 _pthread_body + 126
7 libsystem_pthread.dylib 0x00007fff60d5626f _pthread_start + 70
8 libsystem_pthread.dylib 0x00007fff60d52415 thread_start + 13

Thread 3:
0 libsystem_pthread.dylib 0x00007fff60d523f8 start_wqthread + 0
1 ??? 0x0000000054485244 0 + 1414025796

Thread 4:
0 libsystem_pthread.dylib 0x00007fff60d523f8 start_wqthread + 0
1 ??? 0x0000000000000903 0 + 2307

Thread 1 crashed with X86 Thread State (64-bit):
rax: 0x0000000000000000 rbx: 0x00007000054b9000 rcx: 0x00007000054b88b8 rdx: 0x0000000000000000
rdi: 0x0000000000002703 rsi: 0x0000000000000006 rbp: 0x00007000054b88f0 rsp: 0x00007000054b88b8
r8: 0x00007000054b8778 r9: 0x00007000054b8950 r10: 0x0000000000000000 r11: 0x0000000000000206
r12: 0x0000000000002703 r13: 0x0000003000000008 r14: 0x0000000000000006 r15: 0x000000000000002d
rip: 0x00007fff60c9f23e rfl: 0x0000000000000206 cr2: 0x00007fff93949188

最佳答案

最好的方法是使用 https://github.com/ashleymills/Reachability.swift

在此链接中,您将看到如何始终请求 google.com 服务器。

https://github.com/ashleymills/Reachability.swift/blob/master/ReachabilitySample/ViewController.swift

有很多方法可以做到这一点

let hostReachability = Reachability(hostName:"www.apple.com")
hostReachability.startNotifier()

NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "reachabilityChanged:",
name: kReachabilityChangedNotification,
object: nil)

它将避免您使用 while(true) 或类似难以处理的东西。

enum NetworkStatus {
case notReachable, reachableViaWiFi, reachableViaWWAN
}
var currentReachabilityStatus: NetworkStatus

关于swift - 由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因 : 'Failed to set posix_spawn_file_actions for fd -1 at index 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54405568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com