dduan / Just

Swift HTTP for Humans
MIT License
1.4k stars 134 forks source link

[OS X] Modifying UI in async request raises exception #12

Closed itisbugra closed 9 years ago

itisbugra commented 9 years ago

I need to reload a NSTableView in the end of some asynchronous request made by Just, however it ends up with the error:

2015-08-14 20:55:01.943 ClassTrackerSwift[35619:2707409] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.
 Stack:(
    0   CoreFoundation                      0x00007fff9341303c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff92fd276e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff93412eed +[NSException raise:format:] + 205
    3   Foundation                          0x00007fff9395a4f0 _AssertAutolayoutOnMainThreadOnly + 83
    4   Foundation                          0x00007fff93960391 -[NSISEngine optimize] + 49
    5   Foundation                          0x00007fff9397cbb3 -[NSISEngine constraintDidChangeSuchThatMarker:shouldBeReplacedByMarkerPlusDelta:] + 296
    6   Foundation                          0x00007fff9397ca31 -[NSISEngine tryToChangeConstraintSuchThatMarker:isReplacedByMarkerPlusDelta:undoHandler:] + 435
    7   Foundation                          0x00007fff9394fc5d -[NSLayoutConstraint _tryToChangeContainerGeometryWithUndoHandler:] + 482
    8   Foundation                          0x00007fff9394f5c4 -[NSLayoutConstraint _setSymbolicConstant:constant:] + 427
    9   AppKit                              0x00007fff9106e2a8 -[NSView(NSConstraintBasedLayout) _autoresizingConstraints_frameDidChange] + 236
    10  AppKit                              0x00007fff9106dd39 -[NSView setFrameSize:] + 1433
    11  AppKit                              0x00007fff9106d79a -[NSControl setFrameSize:] + 77
    12  AppKit                              0x00007fff911897a1 -[NSTableView setFrameSize:] + 256
    13  AppKit                              0x00007fff9118a288 -[NSTableView tile] + 433
    14  AppKit                              0x00007fff911a36f9 -[NSTableView bounds] + 113
    15  AppKit                              0x00007fff9104a62e -[NSView(NSInternal) _setLayerNeedsDisplayInViewRect:] + 406
    16  AppKit                              0x00007fff9102dbfc NSViewSetNeedsDisplayInRect + 1014
    17  AppKit                              0x00007fff9118953c -[NSTableView _tileAndRedisplayAll] + 231
    18  ClassTrackerSwift                   0x00000001000452f6 _TFC17ClassTrackerSwift14ViewController11fetchDidEndfS0_FTCS_13CourseFetcher7successSb_T_ + 166
    19  ClassTrackerSwift                   0x0000000100046a5f _TTWC17ClassTrackerSwift14ViewControllerS_21CourseFetcherDelegateS_FS1_11fetchDidEndUS1___fQPS1_FTCS_13CourseFetcher7successSb_T_ + 79
    20  ClassTrackerSwift                   0x0000000100043ac4 _TFFC17ClassTrackerSwift13CourseFetcher5fetchFS0_FSST_U_FGSQCS_10HTTPResult_T_ + 14532
    21  ClassTrackerSwift                   0x0000000100043d77 _TTRXFo_oGSQC17ClassTrackerSwift10HTTPResult__dT__XFo_iGSQS0___iT__ + 23
    22  ClassTrackerSwift                   0x000000010003d9c1 _TPA__TTRXFo_oGSQC17ClassTrackerSwift10HTTPResult__dT__XFo_iGSQS0___iT__ + 81
    23  ClassTrackerSwift                   0x0000000100032b70 _TTRXFo_iGSQC17ClassTrackerSwift10HTTPResult__iT__XFo_oGSQS0___dT__ + 32
    24  ClassTrackerSwift                   0x0000000100032a21 _TFFC17ClassTrackerSwift4Just7requestFS0_FTOS_10HTTPMethod9URLStringSS6paramsGVSs10DictionarySSPSs9AnyObject__4dataGS2_SSPS3___4jsonGSqGS2_SSPS3____7headersGS2_SSSS_5filesGS2_SSOS_8HTTPFile_4authGSqT8usernameSS8passwordSS__7cookiesGS2_SSSS_9redirectsSb7timeoutGSqSd_8URLQueryGSqSS_11requestBodyGSqCSo6NSData_20asyncProgressHandlerGSqFGSQVS_12HTTPProgress_T__22asyncCompletionHandlerGSqFGSQCS_10HTTPResult_T___S7_U_FS7_T_ + 321
    25  ClassTrackerSwift                   0x000000010002fb56 _TPA__TFFC17ClassTrackerSwift4Just7requestFS0_FTOS_10HTTPMethod9URLStringSS6paramsGVSs10DictionarySSPSs9AnyObject__4dataGS2_SSPS3___4jsonGSqGS2_SSPS3____7headersGS2_SSSS_5filesGS2_SSOS_8HTTPFile_4authGSqT8usernameSS8passwordSS__7cookiesGS2_SSSS_9redirectsSb7timeoutGSqSd_8URLQueryGSqSS_11requestBodyGSqCSo6NSData_20asyncProgressHandlerGSqFGSQVS_12HTTPProgress_T__22asyncCompletionHandlerGSqFGSQCS_10HTTPResult_T___S7_U_FS7_T_ + 182
    26  ClassTrackerSwift                   0x0000000100032b97 _TTRXFo_oC17ClassTrackerSwift10HTTPResult_dT__XFo_iS0__iT__ + 23
    27  ClassTrackerSwift                   0x000000010002fbf1 _TPA__TTRXFo_oC17ClassTrackerSwift10HTTPResult_dT__XFo_iS0__iT__ + 81
    28  ClassTrackerSwift                   0x0000000100032d90 _TTRXFo_iC17ClassTrackerSwift10HTTPResult_iT__XFo_oS0__dT__ + 32
    29  ClassTrackerSwift                   0x000000010002dbff _TFC17ClassTrackerSwift4Just10URLSessionfS0_FTCSo12NSURLSession4taskCSo16NSURLSessionTask20didCompleteWithErrorGSqCSo7NSError__T_ + 2575
    30  ClassTrackerSwift                   0x000000010002e0f0 _TToFC17ClassTrackerSwift4Just10URLSessionfS0_FTCSo12NSURLSession4taskCSo16NSURLSessionTask20didCompleteWithErrorGSqCSo7NSError__T_ + 96
    31  CFNetwork                           0x00007fff920337c9 __51-[NSURLSession delegate_task:didCompleteWithError:]_block_invoke235 + 62
    32  Foundation                          0x00007fff9394b7e8 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
    33  Foundation                          0x00007fff9394b5b5 -[NSBlockOperation main] + 97
    34  Foundation                          0x00007fff9394aa6c -[__NSOperationInternal _start:] + 653
    35  Foundation                          0x00007fff9394a543 __NSOQSchedule_f + 184
    36  libdispatch.dylib                   0x0000000100533d43 _dispatch_client_callout + 8
    37  libdispatch.dylib                   0x0000000100537fb3 _dispatch_queue_drain + 1804
    38  libdispatch.dylib                   0x0000000100539fc0 _dispatch_queue_invoke + 223
    39  libdispatch.dylib                   0x0000000100536f5e _dispatch_root_queue_drain + 666
    40  libdispatch.dylib                   0x0000000100548cd0 _dispatch_worker_thread3 + 106
    41  libsystem_pthread.dylib             0x00007fff9bc49637 _pthread_wqthread + 729
    42  libsystem_pthread.dylib             0x00007fff9bc4740d start_wqthread + 13
)

Having found this http://stackoverflow.com/questions/28302019/getting-a-this-application-is-modifying-the-autolayout-engine-error afterwards, however I couldn't solve it as I lack of knowledge about GCD.

As the auto-created log says, it doesn't raise exception (though I wrote it as it does). It enters an infinite loop.

jeffh commented 9 years ago

UI code can only run in one thread (the main thread). It's unsafe perform any UI operation on a background thread, which NSURLSession callbacks default to. So you'll need to move the code that interacts with the UI to the main thread:

dispatch_async(dispatch_get_main_queue()) {
    tableView.reloadData()
}
dduan commented 9 years ago

@Instigater What @jeffh said.

Thank you Jeff ❤️