iOS9此应用程序正在从后台线程修改autolayout引擎,这可能会导致引擎损坏和奇怪的崩溃

我刚刚下载最新的XCode(7.1 beta),并开始玩iOS9.

我有一个应用程序在iOS8中完美无缺,但是现在我在UITableViewCell类中覆盖drawRect方法得到以下错误:

“这个应用程序正在从后台线程中修改autolayout引擎,这可能会导致引擎损坏和奇怪的崩溃,这将在以后的版本中引发异常.

这里是回溯:

Stack:(
0   CoreFoundation                      0x000000010a749f65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000109dcfdeb objc_exception_throw + 48
2   CoreFoundation                      0x000000010a749e9d +[NSException raise:format:] + 205
3   Foundation                          0x0000000109b442e5 _AssertAutolayoutOnMainThreadOnly + 79
4   Foundation                          0x00000001099a4ece -[NSISEngine withBehaviors:performModifications:] + 31
5   UIKit                               0x000000010b9d425b -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 58
6   UIKit                               0x000000010b9d4d9e -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 254
7   UIKit                               0x000000010b702760 -[UITableViewCellContentView updateConstraintsIfNeeded] + 185
8   UIKit                               0x000000010b9d5ab3 -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 272
9   UIKit                               0x000000010b1e6274 -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159
10  UIKit                               0x000000010b1f5d84 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
11  QuartzCore                          0x000000010ae1059a -[CALayer layoutSublayers] + 146
12  QuartzCore                          0x000000010ae04e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
13  QuartzCore                          0x000000010ae04cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14  QuartzCore                          0x000000010adf9475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
15  QuartzCore                          0x000000010ae26c0a _ZN2CA11Transaction6commitEv + 486
16  QuartzCore                          0x000000010ae2737c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
17  CoreFoundation                      0x000000010a675967 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18  CoreFoundation                      0x000000010a6758d7 __CFRunLoopDoObservers + 391
19  CoreFoundation                      0x000000010a66ae4c CFRunLoopRunSpecific + 524
20  CoreFoundation                      0x000000010a71e011 CFRunLoopRun + 97
21  SDWebImage                          0x000000010971773c -[SDWebImageDownloaderOperation start] + 1868
22  Foundation                          0x0000000109961e47 __NSOQSchedule_f + 194
23  libdispatch.dylib                   0x000000010d93849b _dispatch_client_callout + 8
24  libdispatch.dylib                   0x000000010d91e8ec _dispatch_queue_drain + 2215
25  libdispatch.dylib                   0x000000010d91de0d _dispatch_queue_invoke + 601
26  libdispatch.dylib                   0x000000010d920a56 _dispatch_root_queue_drain + 1420
27  libdispatch.dylib                   0x000000010d9204c5 _dispatch_worker_thread3 + 111
28  libsystem_pthread.dylib             0x000000010dc80a9d _pthread_wqthread + 729
29  libsystem_pthread.dylib             0x000000010dc7e3dd start_wqthread + 13
)

这是drawRect方法:

override func drawRect(rect: CGRect) {

    super.drawRect(rect)

    let cellRect = rect

    // Values
    let buttonBoxX = CELL_MARGIN + CELL_MARGIN/2
    let buttonBoxY = cellRect.height - buttonBoxHeight
    let buttonBoxWidth = rect.width - CELL_MARGIN * 3


    // Set Button Box
    let buttonBoxRect = CGRectMake(buttonBoxX,buttonBoxY,buttonBoxWidth,buttonBoxHeight )
    let buttonBox = UIBezierPath(roundedRect: buttonBoxRect,byRoundingCorners: [.BottomRight,.BottomLeft],cornerRadii: CGSize(width: CORNER_RADIUS,height: CORNER_RADIUS)) // Create the path
    UIColor.whiteColor().setFill() // Set the Fill to be white

    buttonBox.fill()


}

我的理解(参见this question)是,复杂的计算等应该在后台线程上完成,并且在主线程上更新UI,因为UI不是线程安全的.

但是,如果我使用以下内容:

override func drawRect(rect: CGRect) {

    super.drawRect(rect)

    let cellRect = rect

    // Values
    let buttonBoxX = CELL_MARGIN + CELL_MARGIN/2
    let buttonBoxY = cellRect.height - buttonBoxHeight
    let buttonBoxWidth = rect.width - CELL_MARGIN * 3


    // Set Button Box
    let buttonBoxRect = CGRectMake(buttonBoxX,height: CORNER_RADIUS)) // Create the path
    UIColor.whiteColor().setFill() // Set the Fill to be white

    dispatch_async(dispatch_get_main_queue(),{
        buttonBox.fill()
    })


}

我现在得到一个CGContext错误…

<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace,please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace,please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextAddPath: invalid context 0x0. If you want to see the backtrace,please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextDrawPath: invalid context 0x0. If you want to see the backtrace,please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace,please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace,please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

有什么建议么 ?

解决方案:
好.这是交易.我正在将图像异步加载到UIImageView中,但不是在主线程的UI中“绘制”它,而是在后台线程上.此外;在将图像添加到UI之后,我在UITableViewCell上调用setNeedsDisplay,从而再次调用drawRect方法,但这一次在后台线程上.

解决方法

你注意到的第一个错误看起来不像你的drawRect:代码.你的drawRect:看起来不错没有做任何特别复杂的事情.如果您的路径更复杂,您可能希望缓存它,但可能原样很好.更有可能您正在尝试修改背景线程上其他位置的UI.当您重写drawRect时,它可能只会弹起来,因为这会改变UI更新的发生方式(没有自定义的drawRect:系统可能可以应用简单的转换).您需要查找在后台线程上进行UIKit调用的位置.

如有疑问,如果以UI开头,您可能无法在后台线程上使用它.这不完全是真的(您可以在后台线程上创建UIBezierPath,甚至可以将其绘制到非屏幕上下文中),但作为第一个近似值,这是一件好事.

这段代码是不正确的:

dispatch_async(dispatch_get_main_queue(),{
    buttonBox.fill()
})

对drawRect的调用:最好已经在主队列中(所以调度到主队列是没有帮助的).如果不是,见上文.在该块执行时,绘制周期结束,所以没有任何上下文可以进行绘制.

以上是来客网为你收集整理的iOS9此应用程序正在从后台线程修改autolayout引擎,这可能会导致引擎损坏和奇怪的崩溃全部内容,希望文章能够帮你解决iOS9此应用程序正在从后台线程修改autolayout引擎,这可能会导致引擎损坏和奇怪的崩溃所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。