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

[已解决]swift代码出现警告:This application is modifying the autolayout engine from a background thread

iOS crifan 755浏览 0评论

代码:

class LoginRegisterViewController: AutoMoveUpViewController, UITextFieldDelegate {
    func loginSuccessfully(){
        gLog.verbose(“”)
        self.clearBeingLogin()
        if gCurUserItem.gesturePassword.isEmpty {
            dispatchMain_async({
                let gesturePasswordVC = GesturePasswordViewController()
                SingletonRootNC().pushViewController(gesturePasswordVC, animated: true)
            })
        } else {
            dispatchMain_async({
                showMainView()
            })
            SingletonHomeVC().getHomeStatisticInfo()
            SingletonHomeVC().getUncompltedTaskList()
            SingletonCustomerVC().getCustomerSource()
            SingletonCustomerVC().getCarSerieList()
            SingletonCustomerVC().getRegionList()
            let memoManagementVC = MemoManagementViewController()
            memoManagementVC.getCustomerdefeat()
        }
    }
}

其中,loginSuccessfully在运行的时候,是处于非主(界面)线程

对于前面几个初始化:

          SingletonHomeVC().getHomeStatisticInfo()
            SingletonHomeVC().getUncompltedTaskList()
            SingletonCustomerVC().getCustomerSource()
            SingletonCustomerVC().getCarSerieList()
            SingletonCustomerVC().getRegionList()

都是正常的,没有什么警告错误。

但是(别人写的):

          let memoManagementVC = MemoManagementViewController()
            memoManagementVC.getCustomerdefeat()

却出现了警告提示:

SalesApp[34505:4106491] 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                      0x01e28494 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0167de02 objc_exception_throw + 50
    2   CoreFoundation                      0x01e283bd +[NSException raise:format:] + 141
    3   Foundation                          0x0144cf4e _AssertAutolayoutOnMainThreadOnly + 96
    4   Foundation                          0x012743c4 -[NSISEngine withBehaviors:performModifications:] + 26
    5   Foundation                          0x0127791c -[NSISEngine withAutomaticOptimizationDisabled:] + 48
    6   UIKit                               0x034e40e8 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 75
    7   UIKit                               0x034e39bc __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 741
    8   UIKit                               0x034e40f0 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 83
    9   UIKit                               0x034e3682 -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 248
    10  UIKit                               0x034e3585 -[UIView(AdditionalLayoutSupport) _parentalLayoutEngineDidChangeTo:] + 55
    11  UIKit                               0x034e3b07 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 249
    12  Foundation                          0x01274440 -[NSISEngine withBehaviors:performModifications:] + 150
    13  Foundation                          0x0127791c -[NSISEngine withAutomaticOptimizationDisabled:] + 48
    14  UIKit                               0x034e40e8 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 75
    15  UIKit                               0x034e39bc __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 741
    16  UIKit                               0x034e40f0 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 83
    17  UIKit                               0x034e3682 -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 248
    18  UIKit                               0x034e304f -[UIView(AdditionalLayoutSupport) _initializeHostedLayoutEngine] + 728
    19  UIKit                               0x034d40c5 -[UIView(UIConstraintBasedLayout) _initializeLayoutEngine] + 98
    20  UIKit                               0x034e4021 -[UIView(AdditionalLayoutSupport) _layoutEngineCreateIfNecessary] + 73
    21  UIKit                               0x034d5032 -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 119
    22  UIKit                               0x034d53e8 -[UIView(UIConstraintBasedLayout) nsli_addConstraint:] + 56
    23  UIKit                               0x034d53a0 -[UIView(UIConstraintBasedLayout) _addConstraint:] + 304
    24  UIKit                               0x034d526b -[UIView(UIConstraintBasedLayout) addConstraint:] + 40
    25  Cartography                         0x008dd340 _TFC11Cartography10Constraint7installfT_T_ + 80
    26  Cartography                         0x008e9308 _TFC11Cartography15ConstraintGroup18replaceConstraintsfGSaCS_10Constraint_T_ + 360
    27  Cartography                         0x008e2ee9 _TF11Cartography9constrainFTCSo6UIView7replaceCS_15ConstraintGroup5blockFVS_11LayoutProxyT__S1_ + 105
    28  SalesApp                            0x00200904 _TFC8SalesApp22LeftLabelTableViewCellcfT13leftLabelTextSS15reuseIdentifierGSqSS_14leftLabelColorCSo7UIColor13leftLabelFontCSo6UIFont11isMandatorySb17leftLabelMaxWidthV12CoreGraphics7CGFloat_S0_ + 2644
    29  SalesApp                            0x000f3823 _TFC8SalesApp36LeftLabelRightTextfieldTableViewCellcfT8editableSb15reuseIdentifierGSqSS_13leftLabelTextSS11isMandatorySb18rightTextFieldTextSS25rightTextFieldPlaceholderSS23rightTextFieldTextColorCSo7UIColor22rightTextFieldTextFontCSo6UIFont22constrainToParentRightV12CoreGraphics7CGFloat_S0_ + 1347
    30  SalesApp                            0x000f4025 _TFC8SalesApp36LeftLabelRightTextfieldTableViewCellCfT8editableSb15reuseIdentifierGSqSS_13leftLabelTextSS11isMandatorySb18rightTextFieldTextSS25rightTextFieldPlaceholderSS23rightTextFieldTextColorCSo7UIColor22rightTextFieldTextFontCSo6UIFont22constrainToParentRightV12CoreGraphics7CGFloat_S0_ + 325
    31  SalesApp                            0x001d8084 _TFC8SalesApp28MemoManagementViewControllercfT15curCustomerItemGSqCS_12CustomerItem_9memotTypeGSqOS_18MemoManagementType_7curtaskGSqCS_8TaskItem_7curMemoGSqCS_4Memo_10isMemoEditGSqSb__S0_ + 3348
    32  SalesApp                            0x001db0a8 _TFC8SalesApp28MemoManagementViewControllerCfT15curCustomerItemGSqCS_12CustomerItem_9memotTypeGSqOS_18MemoManagementType_7curtaskGSqCS_8TaskItem_7curMemoGSqCS_4Memo_10isMemoEditGSqSb__S0_ + 136
    33  SalesApp                            0x002838f5 _TFC8SalesApp27LoginRegisterViewController17loginSuccessfullyfT_T_ + 965
    34  SalesApp                            0x0027d8ff _TFC8SalesApp27LoginRegisterViewController18getUserInfoHandlerfTGO9Alamofire6ResultV10SwiftyJSON4JSONCSo7NSError_13mergedAllParaGVs10DictionarySSPs9AnyObject___T_ + 2975
    35  SalesApp                            0x00284b34 _TPA.119 + 36
    36  SalesApp                            0x00071ef4 _TFFF8SalesApp24getUrlRespDataJson_asyncFTO9Alamofire6Method3urlSS10parametersGSqGVs10DictionarySSPs9AnyObject___7headersGSqGS2_SSSS__9extraParaGSqGS2_SSPS3____15respJsonHandlerFTGOS0_6ResultV10SwiftyJSON4JSONCSo7NSError_13mergedAllParaGS2_SSPS3____T__T_U_FT_T_U_FTGS4_S6_S7__GS2_SSPS3____T_ + 5908
    37  SalesApp                            0x0006822a _TPA__TFFF8SalesApp24getUrlRespDataJson_asyncFTO9Alamofire6Method3urlSS10parametersGSqGVs10DictionarySSPs9AnyObject___7headersGSqGS2_SSSS__9extraParaGSqGS2_SSPS3____15respJsonHandlerFTGOS0_6ResultV10SwiftyJSON4JSONCSo7NSError_13mergedAllParaGS2_SSPS3____T__T_U_FT_T_U_FTGS4_S6_S7__GS2_SSPS3____T_ + 154
    38  SalesApp                            0x0006d3b3 _TFFF8SalesApp20getUrlRespJson_asyncFTO9Alamofire6Method3urlSS10parametersGSqGVs10DictionarySSPs9AnyObject___7headersGSqGS2_SSSS__9extraParaGSqGS2_SSPS3____15respJsonHandlerFTGOS0_6ResultV10SwiftyJSON4JSONCSo7NSError_13mergedAllParaGS2_SSPS3____T__T_U_FGVS0_8ResponsePS3__S7__T_U_FT_T_ + 4787
    39  SalesApp                            0x000692e0 _TPA__TFFF8SalesApp20getUrlRespJson_asyncFTO9Alamofire6Method3urlSS10parametersGSqGVs10DictionarySSPs9AnyObject___7headersGSqGS2_SSSS__9extraParaGSqGS2_SSPS3____15respJsonHandlerFTGOS0_6ResultV10SwiftyJSON4JSONCSo7NSError_13mergedAllParaGS2_SSPS3____T__T_U_FGVS0_8ResponsePS3__S7__T_U_FT_T_ + 432
    40  SalesApp                            0x00080ed8 _TTRXFo__dT__XFdCb__dT__ + 40
    41  libdispatch.dylib                   0x045e1363 _dispatch_call_block_and_release + 15
    42  libdispatch.dylib                   0x046049cd _dispatch_client_callout + 14
    43  libdispatch.dylib                   0x045ea971 _dispatch_root_queue_drain + 1008
    44  libdispatch.dylib                   0x045ea57a _dispatch_worker_thread3 + 115
    45  libsystem_pthread.dylib             0x0493225c _pthread_wqthread + 1050
    46  libsystem_pthread.dylib             0x0492ff56 start_wqthread + 34
)

此处一直很疑惑:

为何其他那几个调用函数没有出现警告:

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

-》通过:

_TFC8SalesApp36LeftLabelRightTextfieldTableViewCellCfT8editableSb15reuseIdentifierGSqSS_13leftLabelTextSS11isMandatorySb18rightTextFieldTextSS25rightTextFieldPlaceholderSS23rightTextFieldTextColorCSo7UIColor22rightTextFieldTextFontCSo6UIFont22constrainToParentRightV12CoreGraphics7CGFloat_S0_ + 325
    31  SalesApp                            0x001d8084 _TFC8SalesApp28MemoManagementViewControllercfT15curCustomerItemGSqCS_12CustomerItem_9memotTypeGSqOS_18MemoManagementType_7curtaskGSqCS_8TaskItem_7curMemoGSqCS_4Memo_10isMemoEditGSqSb__S0_ + 3348

基本上确定出错的地方是:

在对应的类MemoManagementViewController初始化,init的时候:

调用了LeftLabelRightTextfieldTableViewCell,从而导致出现警告的:

在非主的UI界面线程,去操作了UI界面上的东西

-》所以才报错的

-》但是之前那么多的其他的类去调用LeftLabelRightTextfieldTableViewCell,为何却没有出错呢?

最后的最后,终于有点搞明白了:

之所以:

            SingletonHomeVC().getHomeStatisticInfo()
            SingletonHomeVC().getUncompltedTaskList()
            SingletonCustomerVC().getCustomerSource()
            SingletonCustomerVC().getCarSerieList()
            SingletonCustomerVC().getRegionList()

没有出现警告的原因是:

在此处调用之前,SingletonHomeVC和SingletonCustomerVC两个实例,都已经初始化过了:

它们在初始化的时候,都是:

确保了,在主的UI界面线程中去初始化的:

分别是:

func resetHomeVC() -> HomeViewController {
    gLog.debug(“”)
    gHomeVC = HomeViewController()
    gHomeVC.tabBarItem = UITabBarItem(title: “首页”, image: UIImage(named: “tab_home”), tag: MainTabTagHome)
    gHomeVC.tabBarItem.selectedImage = UIImage(named: “tab_home_selected”)
    return gHomeVC
}
func SingletonHomeVC() -> HomeViewController {
    gLog.debug(“”)
    if gHomeVC == nil {
        resetHomeVC()
    }
    return gHomeVC
}

willFinishLaunchingWithOptions->resetGlobalValues->->resetHomeVC

输出是:

2016-06-23 10:07:50.855 [Debug] [main] [GlobalValues.swift:153] resetHomeVC() >

代码:

func resetCustomerVC() -> CustomerViewController {
    gLog.verbose(“”)
    gCustomerVC = CustomerViewController()
    gCustomerVC.tabBarItem = UITabBarItem(title: “客户”, image: UIImage(named: “tab_customer”), tag: MainTabTagCustomer)
    gCustomerVC.tabBarItem.selectedImage = UIImage(named: “tab_customer_selected”)
    return gCustomerVC
}
func SingletonCustomerVC() -> CustomerViewController {
    gLog.verbose(“”)
    if gCustomerVC == nil {
        resetCustomerVC()
    }
    return gCustomerVC
}
class MainViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
       
        self.viewControllers = [SingletonHomeVC(), SingletonCustomerVC(), SingletonTaskVC(), reportVC]
    }
}

MainViewController->viewDidLoad->resetCustomerVC输出:

2016-06-23 10:10:14.147 [Verbose] [main] [GlobalValues.swift:73] resetCustomerVC() >

而对于在loginSuccessfully中执行:

let memoManagementVC = MemoManagementViewController()

时,时处于非主线程

-》

所以内部的init去初始化一些UI相关的内容时,才报此警告的。

-》解决办法也很简单:

确保你的类,此处的MemoManagementViewController在初始化时候,是在主界面线程的

转载请注明:在路上 » [已解决]swift代码出现警告:This application is modifying the autolayout engine from a background thread

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.239 seconds, using 18.85MB memory