最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Xcode9编译项目出错:Terminating app due to uncaught exception NSInternalInconsistencyException reason Only run on the main thread

Xcode crifan 853浏览 0评论

在升级Xcode9之前,代码是好的。

升级后,去在iPhone6模拟器中运行结果挂了:

2017-09-19 17:43:36.043182+0800 SRTDev[62715:15664572] [Bugly]  Trapped uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Only run on the main thread!’
(
    0   CoreFoundation                      0x0000000110e391cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000011297ff41 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000110e3e362 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x000000010fffa523 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 165
    4   UIFoundation                        0x00000001176fe678 -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 1178
    5   UIFoundation                        0x00000001176fe09d -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 2232
    6   UIFoundation                        0x000000011773501e _enableTextViewResizing + 228
    7   UIFoundation                        0x0000000117734d09 -[NSLayoutManager setTextStorage:] + 900
    8   UIFoundation                        0x0000000117764223 -[NSTextStorage addLayoutManager:] + 84
    9   UIKit                               0x000000010df42582 -[UIFieldEditor initWithTextField:textStorage:] + 739
    10  UIKit                               0x000000010eac353b -[UITextField _fieldEditor] + 186
    11  UIKit                               0x000000010eac7c50 -[UITextField _initContentView] + 56
    12  UIKit                               0x000000010eaadee9 -[UITextField initWithFrame:] + 394
    13  SRTDev                              0x000000010c2618cd _T0So11UITextFieldCABSC6CGRectV5frame_tcfcTO + 77
    14  SRTDev                              0x000000010c2609f4 _T0So11UITextFieldCABSC6CGRectV5frame_tcfC + 100
    15  SRTDev                              0x000000010c25f78b _T06SRTDev36LeftLabelRightTextfieldTableViewCellCACSb8editable_SSSg15reuseIdentifierSS04leftC4TextSb11isMandatorySS05rightm5FieldM0SS0pmQ11PlaceholderSo7UIColorC0pmqM5ColorSo6UIFontC0pmqM4Font12CoreGraphics7CGFloatV017constrainToParentD0Sb0N4Boldtcfc + 443
    16  SRTDev                              0x000000010c25efd6 _T06SRTDev36LeftLabelRightTextfieldTableViewCellCACSb8editable_SSSg15reuseIdentifierSS04leftC4TextSb11isMandatorySS05rightm5FieldM0SS0pmQ11PlaceholderSo7UIColorC0pmqM5ColorSo6UIFontC0pmqM4Font12CoreGraphics7CGFloatV017constrainToParentD0Sb0N4BoldtcfC + 358
    17  SRTDev                              0x000000010c115d26 _T06SRTDev29ManageShopVisitViewControllerCAcA0cD6VCTypeO7curType_SS2idtcfc + 2310
    18  SRTDev                              0x000000010c1153c9 _T06SRTDev29ManageShopVisitViewControllerCAcA0cD6VCTypeO7curType_SS2idtcfC + 73
    19  SRTDev                              0x000000010c298d0d _T06SRTDev19LoginViewControllerC15requestUserInfoyyFyAA13SRTHttpResultCcfU_yycfU1_ + 1469
    20  SRTDev                              0x000000010c45eea9 _T0Ix_IyB_TR + 41
    21  libdispatch.dylib                   0x0000000113e7f3f7 _dispatch_call_block_and_release + 12
    22  libdispatch.dylib                   0x0000000113e8043c _dispatch_client_callout + 8
    23  libdispatch.dylib                   0x0000000113e8c499 _dispatch_root_queue_drain + 1444
    24  libdispatch.dylib                   0x0000000113e8be97 _dispatch_worker_thread3 + 132
    25  libsystem_pthread.dylib             0x00000001143435a2 _pthread_wqthread + 1299
    26  libsystem_pthread.dylib             0x000000011434307d start_wqthread + 13
)
=================================================================
Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 62715, TID: 15664586, Thread name: (none), Queue name: com.tencent.bugly.operationQueue (QOS: USER_INTERACTIVE), QoS: 33
Backtrace:
4   SRTDev                              0x000000010c6024bd -[BLYCrashLogic jceModel] + 1703
5   SRTDev                              0x000000010c5f1193 -[BLYCommonUploadLogic main] + 229
6   Foundation                          0x000000010ff34cd6 -[__NSOperationInternal _start:] + 778
7   Foundation                          0x000000010ff30cff __NSOQSchedule_f + 369
8   libdispatch.dylib                   0x0000000113e8043c _dispatch_client_callout + 8
9   libdispatch.dylib                   0x0000000113e86856 _dispatch_continuation_pop + 967
10  libdispatch.dylib                   0x0000000113e84c86 _dispatch_async_redirect_invoke + 780
11  libdispatch.dylib                   0x0000000113e8c1f9 _dispatch_root_queue_drain + 772
12  libdispatch.dylib                   0x0000000113e8be97 _dispatch_worker_thread3 + 132
13  libsystem_pthread.dylib             0x00000001143435a2 _pthread_wqthread + 1299
14  libsystem_pthread.dylib             0x000000011434307d start_wqthread + 13
2017-09-19 17:43:36.056902+0800 SRTDev[62715:15664586] [reports] Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 62715, TID: 15664586, Thread name: (none), Queue name: com.tencent.bugly.operationQueue (QOS: USER_INTERACTIVE), QoS: 33
Backtrace:
4   SRTDev                              0x000000010c6024bd -[BLYCrashLogic jceModel] + 1703
5   SRTDev                              0x000000010c5f1193 -[BLYCommonUploadLogic main] + 229
6   Foundation                          0x000000010ff34cd6 -[__NSOperationInternal _start:] + 778
7   Foundation                          0x000000010ff30cff __NSOQSchedule_f + 369
8   libdispatch.dylib                   0x0000000113e8043c _dispatch_client_callout + 8
9   libdispatch.dylib                   0x0000000113e86856 _dispatch_continuation_pop + 967
10  libdispatch.dylib                   0x0000000113e84c86 _dispatch_async_redirect_invoke + 780
11  libdispatch.dylib                   0x0000000113e8c1f9 _dispatch_root_queue_drain + 772
12  libdispatch.dylib                   0x0000000113e8be97 _dispatch_worker_thread3 + 132
13  libsystem_pthread.dylib             0x00000001143435a2 _pthread_wqthread + 1299
14  libsystem_pthread.dylib             0x000000011434307d start_wqthread + 13
2017-09-19 17:43:37.056881+0800 SRTDev[62715:15664572] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Only run on the main thread!’
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000110e391cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000011297ff41 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000110e3e362 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x000000010fffa523 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 165
    4   UIFoundation                        0x00000001176fe678 -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 1178
    5   UIFoundation                        0x00000001176fe09d -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 2232
    6   UIFoundation                        0x000000011773501e _enableTextViewResizing + 228
    7   UIFoundation                        0x0000000117734d09 -[NSLayoutManager setTextStorage:] + 900
    8   UIFoundation                        0x0000000117764223 -[NSTextStorage addLayoutManager:] + 84
    9   UIKit                               0x000000010df42582 -[UIFieldEditor initWithTextField:textStorage:] + 739
    10  UIKit                               0x000000010eac353b -[UITextField _fieldEditor] + 186
    11  UIKit                               0x000000010eac7c50 -[UITextField _initContentView] + 56
    12  UIKit                               0x000000010eaadee9 -[UITextField initWithFrame:] + 394
    13  SRTDev                              0x000000010c2618cd _T0So11UITextFieldCABSC6CGRectV5frame_tcfcTO + 77
    14  SRTDev                              0x000000010c2609f4 _T0So11UITextFieldCABSC6CGRectV5frame_tcfC + 100
    15  SRTDev                              0x000000010c25f78b _T06SRTDev36LeftLabelRightTextfieldTableViewCellCACSb8editable_SSSg15reuseIdentifierSS04leftC4TextSb11isMandatorySS05rightm5FieldM0SS0pmQ11PlaceholderSo7UIColorC0pmqM5ColorSo6UIFontC0pmqM4Font12CoreGraphics7CGFloatV017constrainToParentD0Sb0N4Boldtcfc + 443
    16  SRTDev                              0x000000010c25efd6 _T06SRTDev36LeftLabelRightTextfieldTableViewCellCACSb8editable_SSSg15reuseIdentifierSS04leftC4TextSb11isMandatorySS05rightm5FieldM0SS0pmQ11PlaceholderSo7UIColorC0pmqM5ColorSo6UIFontC0pmqM4Font12CoreGraphics7CGFloatV017constrainToParentD0Sb0N4BoldtcfC + 358
    17  SRTDev                              0x000000010c115d26 _T06SRTDev29ManageShopVisitViewControllerCAcA0cD6VCTypeO7curType_SS2idtcfc + 2310
    18  SRTDev                              0x000000010c1153c9 _T06SRTDev29ManageShopVisitViewControllerCAcA0cD6VCTypeO7curType_SS2idtcfC + 73
    19  SRTDev                              0x000000010c298d0d _T06SRTDev19LoginViewControllerC15requestUserInfoyyFyAA13SRTHttpResultCcfU_yycfU1_ + 1469
    20  SRTDev                              0x000000010c45eea9 _T0Ix_IyB_TR + 41
    21  libdispatch.dylib                   0x0000000113e7f3f7 _dispatch_call_block_and_release + 12
    22  libdispatch.dylib                   0x0000000113e8043c _dispatch_client_callout + 8
    23  libdispatch.dylib                   0x0000000113e8c499 _dispatch_root_queue_drain + 1444
    24  libdispatch.dylib                   0x0000000113e8be97 _dispatch_worker_thread3 + 132
    25  libsystem_pthread.dylib             0x00000001143435a2 _pthread_wqthread + 1299
    26  libsystem_pthread.dylib             0x000000011434307d start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

好像是之前的:

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Only run on the main thread!’

但是觉得有点怪的是:

为何Xcode8之前编译出来的运行的都好好的,现在Xcode9编译运行却出错呢?

Xcode9 Terminating app due to uncaught exception NSInternalInconsistencyException reason Only run on the main thread

Xcode 9 Terminating app due to uncaught exception NSInternalInconsistencyException reason Only run on the main thread

Terminating app due to uncaught exception NSInternalInconsistencyException reason Only run on the main thread

ios – ‘NSInternalInconsistencyException’, reason: ‘Only run on the main thread!’ error – Stack Overflow

ios – “Only run on the main thread” exception is simulator only – Stack Overflow

[已解决]swift的UITextView的sizeThatFits出错:Terminating app due to uncaught exception NSInternalInconsistencyException reason Only run on the main thread – 在路上

目前看起来貌似是:

这部分的代码:

                    dispatchBackground_async {
                        //SingletonTaskVC().initData()
                        
                        if gCurUserItem.userInfo.jobType == .leader {
                            SingletonTaskVC().curTaskVCType = .shopVisit
                        }else if(gCurUserItem.userInfo.jobType == .MarketingManager){
                            SingletonTaskVC().curTaskVCType = .shopHeadInspection
                        }else{
                            SingletonTaskVC().curTaskVCType = .workCalendar
                        }
                        
                        //SingletonTaskVC().getTaskList()
                        //SingletonTaskVC().getTasksCount()
                        //let communityMeetingVC = CommunityMeetingViewController()
                       // communityMeetingVC.getProcedure()
                        let dealerListViewController = DealerListViewController()
                        dealerListViewController.getDealerList()
                        
                        let addShopVisitVC = ManageShopVisitViewController()
                        addShopVisitVC.getMissionItemList()
                        SingletonNewsVC().getUnReadCount()
                        getOrganization()
                        getReportFilter()
                        getProcedure()
                        //SingletonRepoetVC().addWebView()
                    }

有问题。

然后找到了可能出错的地方,就好办了。

以此去注视掉不同的代码,看看到底问题出在哪里。

另外,无意间发现,好像Xcode9中对出个提示:

DealerListViewController.swift: runtime: UI API called from background thread: -[UIView init] must be used from main thread only

然后点击开Thread 6,可以看到具体错误的地方:

所以,去想办法改掉这个写法

【总结】

此处,最诡异的,不是此处的代码有问题导致app崩溃,

而是有问题的代码,iOS11之前,竟然是可以正常运行的,app也没有崩溃。

而升级到了iOS11后,问题暴露出来,导致app崩溃了。

至今,没有找到为何iOS11之前为何没有导致app崩溃。

感觉是:iOS11之前,对于UI代码运行在非UI线程这个事情,检查的不够严格?

而此处的错误,通过Xcode9的智能提示,直接定位错误位置,很快就可以优化修复代码:

改为:

LoginViewController.swift

//                        let dealerListViewController = DealerListViewController()
//                        dealerListViewController.getDealerList()
                        getDealerList()

DealerListViewController.swift

class DealerListViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    init(curType:DealerListVCType = .singleSelect,dealerCodeList:[String] = [String](),dealerListCompletionHandler:((_ curDealerList:[Dealer]) -> Void)? = nil){
        if self.dealerListCompletionHandler != nil {
            if gInformationStorage.dealerList.count == 0{
                getDealerList(completionHandler: realoadDealerItem)
            }else{
                realoadDealerItem()
            }
        }
    }
}
func getDealerList(completionHandler:(() -> Void)? = nil){
    gInformationStorage.dealerList = [Dealer]()
    getUrlRespJson_async(
        httpMethod: .get,
        url:  ServerApi.getDealerListUrl(),
        parameters: nil,
        respJsonHandle: { (response) in
            if response.isSuccess {
                gLog.debug(response.successValue)
                if let dealerItemJsonArray = response.successValue.array {
                    for itemJson in dealerItemJsonArray {
                        let dealer = Dealer()
                        parseDealerJsonToItem(itemJson, dealerItem: dealer)
                        gInformationStorage.dealerList.append(dealer)
                    }
                    if let completeCallback = completionHandler {
                        completeCallback()
                    }
                }
            } else if response.isFailure {
                // self?.noticeInfo(response.failedMessage)
            }
    })
}

就可以了。

转载请注明:在路上 » 【已解决】Xcode9编译项目出错:Terminating app due to uncaught exception NSInternalInconsistencyException reason Only run on the main thread

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
61 queries in 0.359 seconds, using 18.91MB memory