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

后端 未结 21 1617
爱一瞬间的悲伤
爱一瞬间的悲伤 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:22

    I had this issue while reloading data in UITableView. Simply dispatching reload as follows fixed the issue for me.

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.tableView.reloadData()
        })
    
    0 讨论(0)
  • 2020-11-22 16:22

    Swift 4,

    Suppose, if you are calling some method using operation queue

    operationQueue.addOperation({
                self.searchFavourites()
            })
    

    And suppose function searchFavourites is like,

    func searchFavourites() {
         DispatchQueue.main.async {
                        //Your code
                    }
    }
    

    if you call, all code inside the method "searchFavourites" on the main thread, it will still give an error if you are updating some UI in it.

    This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread.

    So use solution,

    operationQueue.addOperation({
                DispatchQueue.main.async {
                    self.searchFavourites()
                }
            })
    

    For this kind of scenario.

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

    Main problem with "This application is modifying the autolayout engine from a background thread" is that it seem to be logged a long time after the actual problem occurs, this can make it very hard to troubleshoot.

    I managed to solve the issue by creating three symbolic breakpoints.

    Debug > Breakpoints > Create Symbolic Breakpoint...

    Breakpoint 1:

    • Symbol: -[UIView setNeedsLayout]

    • Condition: !(BOOL)[NSThread isMainThread]

    Breakpoint 2:

    • Symbol: -[UIView layoutIfNeeded]

    • Condition: !(BOOL)[NSThread isMainThread]

    Breakpoint 3:

    • Symbol: -[UIView updateConstraintsIfNeeded]

    • Condition: !(BOOL)[NSThread isMainThread]

    With these breakpoints, you can easily get a break on the actual line where you incorrectly call UI methods on non-main thread.

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

    It needs to be placed inside a different thread that allows the UI to update as soon as execution of thread function completes:

    Modern Swift:

    DispatchQueue.main.async {
        // Update UI
    }
    

    Older versions of Swift, pre Swift 3.

    dispatch_async(dispatch_get_main_queue(){
        // code here
    })
    

    Objective-C:

    dispatch_async(dispatch_get_main_queue(), ^{
        // code here
    });
    
    0 讨论(0)
  • 2020-11-22 16:29

    You get similar error message while debugging with print statements without using 'dispatch_async' So when you get that error message, its time to use

    Swift 4

    DispatchQueue.main.async { //code }
    

    Swift 3

    DispatchQueue.main.async(){ //code }
    

    Earlier Swift versions

    dispatch_async(dispatch_get_main_queue()){ //code }
    
    0 讨论(0)
  • 2020-11-22 16:29

    For me, this error message originated from a banner from Admob SDK.

    I was able to track the origin to "WebThread" by setting a conditional breakpoint.

    Then I was able to get rid of the issue by encapsulating the Banner creation with:

    dispatch_async(dispatch_get_main_queue(), ^{
       _bannerForTableFooter = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
       ...
    }
    

    I don't know why this helped as I cannot see how this code was called from a non-main-thread.

    Hope it can help anyone.

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