シェル(@takasqr)のブログ

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 関連の操作                                
}

この記事を書いた人

@takasqr アプリケーション開発が大好きなエンジニア。Vue、Swift、Electrom などでアプリを作って公開している。AWS や Firebase などのクラウドサービスも好き。

作ったアプリKeyScript