当我点击一个需要带我其他viewcontroller的按钮时,Swift iOS应用程序崩溃了

时间:2015-09-29 23:24:22

标签: swift

我正在开发一个Swift iOS应用程序,当我尝试点击登录按钮时,它需要从MySQL数据库中提取用户信息,并且必须使用用户提供的值进行验证。我正在做所有这些事情。在验证凭据后,我需要导航到另一个视图控制器,但不幸的是我的应用程序此时崩溃并给出以下错误。

2015-09-29 18:15:43.839 iOSBudgetManager[2998:66920] *** Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /SourceCache/UIKit_Sim/UIKit-3347.44.2/Keyboard/UIKeyboardTaskQueue.m:374
2015-09-29 18:15:43.867 iOSBudgetManager[2998:66920] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e67cc65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000110429bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010e67caca +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x000000010ed5b98f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   UIKit                               0x000000010f8b57d6 -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 151
    5   UIKit                               0x000000010f356912 -[UIKeyboardImpl setDelegate:force:] + 473
    6   UIKit                               0x000000010f6014ad -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1002
    7   UIKit                               0x000000010f609834 -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 504
    8   UIKit                               0x000000010f2994f1 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 623
    9   UIKit                               0x000000010f29a76e -[UIViewController _presentViewController:withAnimationController:completion:] + 3079
    10  UIKit                               0x000000010f29c6c1 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132
    11  UIKit                               0x000000010f29c5e5 -[UIViewController presentViewController:animated:completion:] + 229
    12  iOSBudgetManager                    0x000000010df507c0 _TFFC16iOSBudgetManager14ViewController13WelcomesigninFS0_FCSo8UIButtonT_U_FTGSQCSo6NSData_GSQCSo13NSURLResponse_GSQCSo7NSError__T_ + 6752
    13  iOSBudgetManager                    0x000000010df50c03 _TTRXFo_oGSQCSo6NSData_oGSQCSo13NSURLResponse_oGSQCSo7NSError__dT__XFo_iTGSQS__GSQS0__GSQS1____iT__ + 51
    14  iOSBudgetManager                    0x000000010df4c131 _TPA__TTRXFo_oGSQCSo6NSData_oGSQCSo13NSURLResponse_oGSQCSo7NSError__dT__XFo_iTGSQS__GSQS0__GSQS1____iT__ + 81
    15  iOSBudgetManager                    0x000000010df50c33 _TTRXFo_iTGSQCSo6NSData_GSQCSo13NSURLResponse_GSQCSo7NSError___iT__XFo_oGSQS__oGSQS0__oGSQS1___dT__ + 35
    16  iOSBudgetManager                    0x000000010df50c9a _TTRXFo_oGSQCSo6NSData_oGSQCSo13NSURLResponse_oGSQCSo7NSError__dT__XFdCb_dGSQS__dGSQS0__dGSQS1___dT__ + 90
    17  CFNetwork                           0x00000001117d8beb __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 157
    18  Foundation                          0x000000010ed7f57f __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
    19  Foundation                          0x000000010ecc00b2 -[NSBlockOperation main] + 98
    20  Foundation                          0x000000010eca2774 -[__NSOperationInternal _start:] + 645
    21  Foundation                          0x000000010eca2383 __NSOQSchedule_f + 184
    22  libdispatch.dylib                   0x0000000110b2b614 _dispatch_client_callout + 8
    23  libdispatch.dylib                   0x0000000110b126a7 _dispatch_queue_drain + 2176
    24  libdispatch.dylib                   0x0000000110b11cc0 _dispatch_queue_invoke + 235
    25  libdispatch.dylib                   0x0000000110b153b9 _dispatch_root_queue_drain + 1359
    26  libdispatch.dylib                   0x0000000110b16b17 _dispatch_worker_thread3 + 111
    27  libsystem_pthread.dylib             0x0000000110e98a9d _pthread_wqthread + 729
    28  libsystem_pthread.dylib             0x0000000110e963dd start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException 

1 个答案:

答案 0 :(得分:0)

这里的线索是错误指出您的代码正在尝试从主线程以外的线程更新应用程序的UI。

  

2015-09-29 18:15:43.867 iOSBudgetManager [2998:66920]终止   应用程序由于未捕获的异常'NSInternalInconsistencyException',   原因:' - [UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished] 可能只   从主线程中调用。'

验证凭据后,我需要导航到其他视图控制器,但不幸的是我的应用程序此时崩溃并发出以下错误。

无论您何时调用代码转换到新的视图控制器,您都需要在对主线程的dispatch_async调用中包装该调用 - 请参阅下面的代码。

dispatch_async(dispatch_get_main_queue(), { 
    //View controller code  
    self.presentViewController(yourNewViewController, animated: true, completion: nil)
})

如果您需要更多详细信息,请阅读Apple的Grand Central Dispatch (GCD) Reference