我正在尝试使用以下堆栈跟踪来呈现ViewController但崩溃了。 有人可以检查和帮助。
presentViewController的代码
func moveToChatView(){
SwiftSpinner.show(Strings.loading)
let destViewController:GroupChatViewController = UIStoryboard(name: "GroupChat", bundle: nil).instantiateViewControllerWithIdentifier("GroupChatViewController") as! GroupChatViewController
destViewController.currentRiderRideID = self.riderRideId!
if NSThread.isMainThread() == true{
self.presentViewController(destViewController, animated: true, completion: nil)
}else{
dispatch_sync(dispatch_get_main_queue()){
self.presentViewController(destViewController, animated: true, completion: nil)
}
}
}
致命异常:NSRangeException 0 CoreFoundation 0x182b482d8 exceptionPreprocess 1 libobjc.A.dylib 0x1948140e4 objc_exception_throw 2 CoreFoundation 0x182a2f4c0 CFStringConvertNSStringEncodingToEncoding 3 UIKit 0x1879e01b4 - [UINib instantiateWithOwner:options:] 4 UIKit 0x1878dc318 - [UIViewController _loadViewFromNibNamed:bundle:] 5 UIKit 0x1875c09bc - [UIViewController loadViewIfRequired] 6 UIKit 0x1875c0928 - [UIViewController视图] 7 UIKit 0x187cb618c - [_ UIFullscreenPresentationController _setPresentedViewController:] 8 UIKit 0x1878c60dc - [UIPresentationController initWithPresentedViewController:presentsViewController:] 9 UIKit 0x1878e2378 - [UIViewController _presentViewController:withAnimationController:completion:] 10 UIKit 0x1878e48c8 __62- [UIViewController presentViewController:animated:completion:] _ block_invoke 11 UIKit 0x1876ae0ec - [UIViewController presentViewController:animated:completion:] 12 Quickride 0x100451968部分适用于LiveRideMapViewController。(moveToChatView() - >())。(闭包#1)(LiveRideMapViewController.swift:1868) 13 libdispatch.dylib 0x194e91954 _dispatch_client_callout 14 libdispatch.dylib 0x194e9f590 _dispatch_barrier_sync_f_slow_invoke 15 libdispatch.dylib 0x194e91954 _dispatch_client_callout 16 libdispatch.dylib 0x194e9620c _dispatch_main_queue_callback_4CF 17 CoreFoundation 0x182aff7f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE 18 CoreFoundation 0x182afd8a0 __CFRunLoopRun 19 CoreFoundation 0x182a292d4 CFRunLoopRunSpecific 20 GraphicsServices 0x18c47f6fc GSEventRunModal 21 UIKit 0x187626f40 UIApplicationMain 22 Quickride 0x100259a70 main(AppDelegate.swift:23) 23 libdyld.dylib 0x194ebea08 start
答案 0 :(得分:0)
通常不使用dispatch_ 同步,而是使用dispatch_ async ,而不必检查主线程是否在线程,如果它是一个UI操作,并且它有可能在一个反向线程上,只需将dispatch_ async 发送到主队列,该主队列将在主队列的下一个运行循环上执行UI操作,用户界面将是“顺畅的”。试试这个:
func moveToChatView(){
SwiftSpinner.show(Strings.loading)
let destViewController:GroupChatViewController = UIStoryboard(name:"GroupChat", bundle: nil).instantiateViewControllerWithIdentifier("GroupChatViewController") as! GroupChatViewController
destViewController.currentRiderRideID = self.riderRideId!
dispatch_async(dispatch_get_main_queue()){
self.presentViewController(destViewController, animated: true,completion:nil)
}
}