iOS13で発生するクラッシュ NSInternalInconsistencyException
- iOS
 - 
                        
2020-02-27                                                                     
この記事は最終更新日から1年以上経過しています。
                    
                    GoogleMapを使用したiOSアプリを使っていて、地図を表示しているときに急にクラッシュするようになったので原因を調べました。
発生するエラーの内容は以下の通り
Fatal Exception: NSInternalInconsistencyException Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.
iOS13で内部仕様が変わったことによるもののようです。
どうもバックグラウンドスレッドからUIを触るなということらしいですが、
そんなことはしていないつもりでした。
おそらくこれまでよりも厳密になったのでしょうか…。
とにかく対応するしかありません。
元のソースコード
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: URLRequest(url: Foundation.URL(string: url)!), completionHandler: { (data, response, error) in
    if let urlContent = data {
        print(urlContent)
    }
})
task.resume()
修正したコード
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: URLRequest(url: Foundation.URL(string: url)!), completionHandler: { (data, response, error) in
    if let urlContent = data {
        DispatchQueue.main.sync {
            print(urlContent)
        }
    }
})
task.resume()
                    
                    
                    
                
                        この記事がお役に立ちましたらシェアお願いします
                    
                    
                
                        9,778 views 
                                                                    
                    
                



