Reputation: 33
I am trying to presentViewController but to crashed with following stack trace. Can some one please check and help.
Code to 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)
}
}
}
Fatal Exception: 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 view] 7 UIKit 0x187cb618c -[_UIFullscreenPresentationController _setPresentedViewController:] 8 UIKit 0x1878c60dc -[UIPresentationController initWithPresentedViewController:presentingViewController:] 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 partial apply for LiveRideMapViewController.(moveToChatView() -> ()).(closure #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
Upvotes: 1
Views: 2619
Reputation: 2802
As a rule don't use dispatch_sync , but instead use dispatch_async and you don't have to check if you on the main thread , if it is a UI operation and there is a chance that it will be on a back thread just do dispatch_async to main queue which will perform the UI operation on the next run loop on the main queue and the UI will be 'smooth' ,try this:
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)
}
}
Upvotes: 1