I have three view controllers in my project. Let's call them view1, view2 and view3. View2 is loaded from View1 using segue from interface builder. But view3 needs to be loaded programmatically from view2 since view2 makes RESTful request to REST server and that request is asynchronous. I have a separate class to handle REST request. Once I get the return value from REST server, I invoke loadResultViewController method of view2. However I'm getting "libc++abi.dylib: terminating with uncaught exception of type NSException" error. How do I resolve this issue?
This is my loadResultViewController method
func loadResultViewController(summary: String) {
let resultViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ResultView") as! ResultViewController
self.presentViewController(resultViewController, animated: true, completion: nil)
resultViewController.summary.text = summary
}
EDIT:
This is full error message.
2015-09-14 14:01:13.645 My Strategic[5307:791826] * Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /SourceCache/UIKit_Sim/UIKit-3347.44.2/Keyboard/UIKeyboardTaskQueue.m:374 2015-09-14 14:01:13.678 My Strategic[5307:791826] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.' *** First throw call stack: ( 0 CoreFoundation 0x000000010345dc65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000104fc8bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010345daca +[NSException raise:format:arguments:] + 106 3 Foundation 0x00000001038fa98f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 4 UIKit 0x00000001044547d6 -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 151 5 UIKit 0x0000000103ef5912 -[UIKeyboardImpl setDelegate:force:] + 473 6 UIKit 0x00000001041a04ad -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1002 7 UIKit 0x00000001041a8834 -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 504 8 UIKit 0x0000000103e384f1 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 623 9 UIKit 0x0000000103e3976e -[UIViewController _presentViewController:withAnimationController:completion:] + 3079 10 UIKit 0x0000000103e3b6c1 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132 11 UIKit 0x0000000103e3b5e5 -[UIViewController presentViewController:animated:completion:] + 229 12 My Strategic 0x0000000102dbfc26 _TFC14My_Strategic22PostCodeViewController24loadResultViewControllerfS0_FSST_ + 870 13 My Strategic 0x0000000102dc0cae _TTWC14My_Strategic22PostCodeViewControllerS_12RestDelegateS_FS1_24loadResultViewControllerUS1___fQPS1_FSST_ + 94 14 My Strategic 0x0000000102dcfcf0 _TFFC14My_Strategic4Rest9getRidingFS0_FSST_U_FTGSQCSo13NSURLResponse_GSQCSo6NSData_GSQCSo7NSError__T_ + 2960 15 My Strategic 0x0000000102dcfe4a _TTRXFo_oGSQCSo13NSURLResponse_oGSQCSo6NSData_oGSQCSo7NSError__dT__XFdCb_dGSQS__dGSQS0__dGSQS1___dT + 90 16 CFNetwork 0x000000010566d8c5 67+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]_block_invoke_2 + 155 17 Foundation 0x000000010391e57f __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 7 18 Foundation 0x000000010385f0b2 -[NSBlockOperation main] + 98 19 Foundation 0x0000000103841774 -[__NSOperationInternal _start:] + 645 20 Foundation 0x0000000103841383 __NSOQSchedule_f + 184 21 libdispatch.dylib 0x0000000106ac2614 _dispatch_client_callout + 8 22 libdispatch.dylib 0x0000000106aa96a7 _dispatch_queue_drain + 2176 23 libdispatch.dylib 0x0000000106aa8cc0 _dispatch_queue_invoke + 235 24 libdispatch.dylib 0x0000000106aac3b9 _dispatch_root_queue_drain + 1359 25 libdispatch.dylib 0x0000000106aadb17 _dispatch_worker_thread3 + 111 26 libsystem_pthread.dylib 0x0000000106e2f637 _pthread_wqthread + 729 27 libsystem_pthread.dylib 0x0000000106e2d40d start_wqthread + 13 ) libc++abi.dylib: terminating with uncaught exception of type NSException
small typo in the suggestion.. the expression block should be prefixed by '^' like below
This is the relevant part of the error message:
You're making the REST request on a background thread and then when you get the result, you perform the transition on the same thread. All UI code should generally run on the main thread.
Try wrapping the code in the method body in this: