Getting a “This application is modifying the autolayout engine from a background thread” error?

后端 未结 21 1644
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 15:52

Been encountering this error a lot in my OS X using swift:

\"This application is modifying the autolayout engine from a background thread, which can

相关标签:
21条回答
  • 2020-11-22 16:31

    I had this problem since updating to the iOS 9 SDK when I was calling a block that did UI updates within an NSURLConnection async request completion handler. Putting the block call in a dispatch_async using dispatch_main_queue solved the issue.

    It worked fine in iOS 8.

    0 讨论(0)
  • 2020-11-22 16:31

    You already have the correct code answer from @Mark but, just to share my findings: The issue is that you are requesting a change in the view and assuming that it will happen instantly. In reality, the loading of a view depends on the available resources. If everything loads quickly enough and there are no delays then you don't notice anything. In scenarios, where there is any delay due to the process thread being busy etc, the application runs into a situation where it is supposed to display something even though its not ready yet. Hence, it is advisable to dispatch these requests in a asynchronous queues so, they get executed based on the load.

    0 讨论(0)
  • 2020-11-22 16:32

    Had the same problem because I was using performSelectorInBackground.

    0 讨论(0)
  • 2020-11-22 16:37

    Here check out this line from the logs

    $S12AppName18ViewControllerC11Func()ySS_S2StF + 4420
    

    you can check that which function calling from the either background thread or where you are calling api method you need to call your function from the main thread like this.

    DispatchQueue.main.async { func()}
    

    func() is that function you want to call in the result of api call success or else.

    Logs Here

    This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
     Stack:(
        0   Foundation                          0x00000001c570ce50 <redacted> + 96
        1   Foundation                          0x00000001c5501868 <redacted> + 32
        2   Foundation                          0x00000001c5544370 <redacted> + 540
        3   Foundation                          0x00000001c5543840 <redacted> + 396
        4   Foundation                          0x00000001c554358c <redacted> + 272
        5   Foundation                          0x00000001c5542e10 <redacted> + 264
        6   UIKitCore                           0x00000001f20d62e4 <redacted> + 488
        7   UIKitCore                           0x00000001f20d67b0 <redacted> + 36
        8   UIKitCore                           0x00000001f20d6eb0 <redacted> + 84
        9   Foundation                          0x00000001c571d124 <redacted> + 76
        10  Foundation                          0x00000001c54ff30c <redacted> + 108
        11  Foundation                          0x00000001c54fe304 <redacted> + 328
        12  UIKitCore                           0x00000001f151dc0c <redacted> + 156
        13  UIKitCore                           0x00000001f151e0c0 <redacted> + 152
        14  UIKitCore                           0x00000001f1514834 <redacted> + 868
        15  UIKitCore                           0x00000001f1518760 <redacted> + 104
        16  UIKitCore                           0x00000001f1543370 <redacted> + 1772
        17  UIKitCore                           0x00000001f1546598 <redacted> + 120
        18  UIKitCore                           0x00000001f14fc850 <redacted> + 1452
        19  UIKitCore                           0x00000001f168f318 <redacted> + 196
        20  UIKitCore                           0x00000001f168d330 <redacted> + 144
        21  AppName                        0x0000000100b8ed00 $S12AppName18ViewControllerC11Func()ySS_S2StF + 4420
        22  AppName                        0x0000000100b8d9f4 $S12CcfU0_y10Foundation4DataVSg_So13NSURLResponseCSgs5Error_pSgtcfU_ + 2384
        23  App NAme                        0x0000000100a98f3c $S10Foundation4DataVSgSo13NSURLResponseCSgs5Error_pSgIegggg_So6NSDataCSgAGSo7NSErrorCSgIeyByyy_TR + 316
        24  CFNetwork                           0x00000001c513aa00 <redacted> + 32
        25  CFNetwork                           0x00000001c514f1a0 <redacted> + 176
        26  Foundation                          0x00000001c55ed8bc <redacted> + 16
        27  Foundation                          0x00000001c54f5ab8 <redacted> + 72
        28  Foundation                          0x00000001c54f4f8c <redacted> + 740
        29  Foundation                          0x00000001c55ef790 <redacted> + 272
        30  libdispatch.dylib                   0x000000010286f824 _dispatch_call_block_and_release + 24
        31  libdispatch.dylib                   0x0000000102870dc8 _dispatch_client_callout + 16
        32  libdispatch.dylib                   0x00000001028741c4 _dispatch_continuation_pop + 528
        33  libdispatch.dylib                   0x0000000102873604 _dispatch_async_redirect_invoke + 632
        34  libdispatch.dylib                   0x00000001028821dc _dispatch_root_queue_drain + 376
        35  libdispatch.dylib                   0x0000000102882bc8 _dispatch_worker_thread2 + 156
        36  libsystem_pthread.dylib             0x00000001c477917c _pthread_wqthread + 472
        37  libsystem_pthread.dylib             0x00000001c477bcec start_wqthread + 4
    )
    
    0 讨论(0)
  • 2020-11-22 16:38

    When you try to update a text field value or adding a subview inside a background thread, you can get this problem. For that reason, you should put this kind of code in the main thread.

    You need to wrap methods that call UI updates with dispatch_asynch to get the main queue. For example:

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
       self.friendLabel.text = "You are following \(friendCount) accounts"
    })
    

    EDITED - SWIFT 3:

    Now, we can do that following the next code:

    // Move to a background thread to do some long running work
    DispatchQueue.global(qos: .userInitiated).async {
       // Do long running task here
       // Bounce back to the main thread to update the UI
       DispatchQueue.main.async {
          self.friendLabel.text = "You are following \(friendCount) accounts"
       }
    }
    
    0 讨论(0)
  • 2020-11-22 16:39

    The "this application is modifying the autolayout engine from a background thread" error is logged in the console long after the actual problem occured, so debugging this can be hard without using a breakpoint.

    I used @markussvensson's answer to detect my problem and found it using this Symbolic Breakpoint (Debug > Breakpoints > Create Symbolic Breakpoint):

    1. Symbols: [UIView layoutIfNeeded] or [UIView updateConstraintsIfNeeded]
    2. Condition: !(BOOL)[NSThread isMainThread]

    Build and run the app on the emulator and replicate the steps that lead to the error message being thrown (the app will be slower than usual!). Xcode will then stop the app and mark the line of code (e.g. the call of a func) that's accessing the UI from a background thread.

    0 讨论(0)
提交回复
热议问题