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

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

    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.

提交回复
热议问题