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

[已解决]swift代码出错:This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes

Swift crifan 684浏览 0评论

出错,log非常多:

2016-04-27 20:39:00.619 [Debug] [com.apple.root.background-qos] [GlobalFunctions.swift:331] calcTimeStart > StoreConversationItemList
2016-04-27 20:39:00.646 [Info] [com.apple.root.background-qos] [ConversationViewController.swift:51] reallyStoreData() > save conversationItemList true for 15 items to /Users/crifan/Library/Developer/CoreSimulator/Devices/63F89987-3382-42A5-BC13-AE102BEF98DB/data/Containers/Data/Application/A130B9B6-1014-406E-9B9F-E3A4D0C9956E/Documents/user-08da3990-9708-4338-ba5e-cff0586e0af8/Conversation/conversationItemList
2016-04-27 20:39:00.647 [Debug] [com.apple.root.background-qos] [GlobalFunctions.swift:342] calcTimeEnd > StoreConversationItemList elapsedTime=0.0271
2016-04-27 20:39:06.158 JianDao[7200:335942] 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                      0x032e0494 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x045a4e02 objc_exception_throw + 50
    2   CoreFoundation                      0x032e03bd +[NSException raise:format:] + 141
    3   Foundation                          0x0273df4e _AssertAutolayoutOnMainThreadOnly + 96
    4   Foundation                          0x025653c4 -[NSISEngine withBehaviors:performModifications:] + 26
    5   Foundation                          0x0256891c -[NSISEngine withAutomaticOptimizationDisabled:] + 48
    6   UIKit                               0x015bf0e8 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 75
    7   UIKit                               0x015bfd61 -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 278
    8   UIKit                               0x015c0c4c -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 372
    9   UIKit                               0x00c6757e -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 173
    10  UIKit                               0x00c7940a -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 864
    11  libobjc.A.dylib                     0x045b9059 -[NSObject performSelector:withObject:] + 70
    12  QuartzCore                          0x04490096 -[CALayer layoutSublayers] + 144
    13  QuartzCore                          0x044838b6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
    14  QuartzCore                          0x0448371a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    15  QuartzCore                          0x04475ee7 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317
    16  QuartzCore                          0x044aa847 _ZN2CA11Transaction6commitEv + 561
    17  QuartzCore                          0x044aabef _ZN2CA11Transaction14release_threadEPv + 289
    18  libsystem_pthread.dylib             0x0559b29d _pthread_tsd_cleanup + 537
    19  libsystem_pthread.dylib             0x0559ae22 _pthread_exit + 108
    20  libsystem_pthread.dylib             0x05599354 pthread_attr_getschedpolicy + 0
    21  libsystem_pthread.dylib             0x05596f56 start_wqthread + 34
)
2016-04-27 20:39:06.161 JianDao[7200:335942] 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                      0x032e0494 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x045a4e02 objc_exception_throw + 50
    2   CoreFoundation                      0x032e03bd +[NSException raise:format:] + 141
    3   Foundation                          0x0273df4e _AssertAutolayoutOnMainThreadOnly + 96
    4   Foundation                          0x025653c4 -[NSISEngine withBehaviors:performModifications:] + 26
    5   Foundation                          0x0256891c -[NSISEngine withAutomaticOptimizationDisabled:] + 48
    6   UIKit                               0x015bf0e8 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 75
    7   UIKit                               0x015bfb4d -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 149
    8   UIKit                               0x015c02b0 __60-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]_block_invoke + 113
    9   Foundation                          0x02565440 -[NSISEngine withBehaviors:performModifications:] + 150
    10  Foundation                          0x0256891c -[NSISEngine withAutomaticOptimizationDisabled:] + 48
    11  UIKit                               0x015bf0e8 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 75
    12  UIKit                               0x015bfd61 -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 278
    13  UIKit                               0x015c0c4c -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 372
    14  UIKit                               0x00c6757e -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 173
    15  UIKit                               0x00c7940a -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 864
    16  libobjc.A.dylib                     0x045b9059 -[NSObject performSelector:withObject:] + 70
    17  QuartzCore                          0x04490096 -[CALayer layoutSublayers] + 144
    18  QuartzCore                          0x044838b6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
    19  QuartzCore                          0x0448371a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    20  QuartzCore                          0x04475ee7 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317
    21  QuartzCore                          0x044aa847 _ZN2CA11Transaction6commitEv + 561
    22  QuartzCore                          0x044aabef _ZN2CA11Transaction14release_threadEPv + 289
    23  libsystem_pthread.dylib             0x0559b29d _pthread_tsd_cleanup + 537
    24  libsystem_pthread.dylib             0x0559ae22 _pthread_exit + 108
    25  libsystem_pthread.dylib             0x05599354 pthread_attr_getschedpolicy + 0
    26  libsystem_pthread.dylib             0x05596f56 start_wqthread + 34
)
。。。
2016-04-27 20:39:16.506 [Debug] [main] [MainViewController.swift:541] websocketKeepConnect() > gCurUserItem.hasLogined=true, gCurUserItem.wsInited=true

之前遇到过2次类似问题:

[已解决]swift中代码运行出错:This application is modifying the autolayout engine from a background thread

[已解决]swift调用CNContactStore的requestAccessForEntityType时出错:This application is modifying the autolayout engine from a background thread

也都是把对应的内容改为

放到主线程,即可。

但是此处不知道哪里的原因导致此问题。

application is modifying the autolayout engine from a background thread解决方案 – SomeBod_Y – 博客园

This application is modifying the autolayout en… | Apple Developer Forums

最后调试了半天,终于找到原因了:

此处的:

                //switch to Main View Controller
                resetMainVC()
                showMainView()

两个都是和视图控制器相关的:

//Note: makesure call this in main thread !!!
func resetMainVC() -> MainViewController {
    gLog.debug("")
    gMainVC = MainViewController()
    return gMainVC
}
//Note: makesure call this in main thread !!!
func showMainView(){
    gLog.debug("")
    setNaviRootVC(SingletonMainVC())
}
//Note: makesure call this in main thread !!!
func setNaviRootVC(rootVC:UIViewController){
    gLog.debug("rootVC=\(rootVC)")
    SingletonRootNC().setViewControllers([rootVC], animated: false)
//    SingletonRootNC().viewControllers = [rootVC]
}

都必须保证,要在主线程中调用

否则就会出现上面的警告。。。

哎:

iOS和Xcode,还是不够智能:

没法提示,到底是哪个函数,被在非主线程中调用

-》只能自己一点点调试自己刚刚修改的代码,找到具体是哪里才行。。。

-》此处就是把上面的代码,确保在,属于主线程的didFinishLaunchingWithOptions中调用,或者是:

func dispatchMain_async(thingsTodo:()->()) {
    let mainQueue = dispatch_get_main_queue()
    dispatch_async(mainQueue, thingsTodo)
}
            dispatchMain_async({
                //switch to Main View Controller
                resetMainVC()
                showMainView()
            })

才可以。

转载请注明:在路上 » [已解决]swift代码出错:This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
64 queries in 0.127 seconds, using 18.86MB memory