2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

画面遷移時にMain Thread Checkerのエラーが出た時の対処法(Swift)

Posted at

はじめに

画面遷移などをする際に下記のようなエラーが出た時の対処法を紹介します。

エラー内容
"Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread"
"Main Thread Checker: UI API called on a background thread"

対処法 (DispatchQueue.main)

DispatchQueue.main.sync {} もしくは DispatchQueue.main.async {} の中に対象のコードを入れる

同期処理の場合

DispatchQueue.main.sync {
  navigationController?.popViewController(animated: true)  ←ここに画面遷移などのコードを記載
}

非同期処理の場合

DispatchQueue.main.async {
  navigationController?.popViewController(animated: true)  ←ここに画面遷移などのコードを記載
}

原因

UIの更新はメインスレッドで行う必要があるため、DispatchQueue.mainに処理を追加しないといけないとのこと。
https://1000ch.net/posts/2016/dispatch-queue.html
https://developer.apple.com/documentation/dispatch/dispatchqueue

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?