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

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

    I also encountered this problem, seeing a ton of these messages and stack traces being printed in the output, when I resized the window to a smaller size than its initial value. Spending a long time figuring out the problem, I thought I'd share the rather simple solution. I had once enabled Can Draw Concurrently on an NSTextView through IB. That tells AppKit that it can call the view's draw(_:) method from another thread. After disabling it, I no longer got any error messages. I didn't experience any problems before updating to macOS 10.14 Beta, but at the same time, I also started modifying the code to perform work with the text view.

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

    It could be something as simple as setting a text field / label value or adding a subview inside a background thread, which may cause a field's layout to change. Make sure anything you do with the interface only happens in the main thread.

    Check this link: https://forums.developer.apple.com/thread/7399

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

    I had this issue when I was using TouchID if that helps anyone else, wrap your success logic which likely does something with the UI in the main queue.

    0 讨论(0)
  • I had the same problem. Turns out I was using UIAlerts that needed the main queue. But, they've been deprecated.
    When I changed the UIAlerts to the UIAlertController, I no longer had the problem and did not have to use any dispatch_async code. The lesson - pay attention to warnings. They help even when you don't expect it.

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

    Obviously you are doing some UI update on back ground thread. Cant predict exactly where, without seeing your code.

    These are some situations it might happen:-

    you might be doing something on background thread and not using. Being in the same function this code is easier to spot.

    DispatchQueue.main.async { // do UI update here }
    

    calling a func doing web request call on background thread and its completion handler calling other func doing ui update. to solve this try checking code where you have updated the UI after webrequest call.

    // Do something on background thread
    DispatchQueue.global(qos: .userInitiated).async {
       // update UI on main thread
       DispatchQueue.main.async {
                    // Updating whole table view
                    self.myTableview.reloadData()
                }
    }
    
    0 讨论(0)
  • 2020-11-22 16:21

    For me the issue was the following. Make sure performSegueWithIdentifier: is performed on the main thread:

    dispatch_async (dispatch_get_main_queue(), ^{
      [self performSegueWithIdentifier:@"ViewController" sender:nil];
    });
    
    0 讨论(0)
提交回复
热议问题