SwiftUI バックグラウンドで View 関連を操作する

公開日:

SwiftUI でアプリを作っていて、バックグラウンドの処理が終わった時などに View 側の見た目を変更したい時があると思います。

その時にバックグラウンドから直接変更すると警告が出ます。

Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

その時は以下のようにメインスレッドから View を変更します。

DispatchQueue.main.async {
    // View 関連の操作                                
}