0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Swift6でUNUserNotificationCenterのaddでアプリがクラッシュする

Posted at

はじめに

Swift6にしてから、UNUserNotificationCenter.current()のaddメソッドでクラッシュが発生するようになりました。
対象アプリ:iOS
Xcodeバージョン:16.2

問題の箇所

以下のコードのcenter.add()の箇所でIncorrect actor executor assumptionが発生したりしてクラッシュしました。

let request = UNNotificationRequest(identifier: "example", content: content, trigger: nil)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: { _ in
    // 何かしらの処理
})

原因はこちらの記事でも書かれていますが、Swift 6 のコンパイラがクロージャの実行スレッドを誤ってメインアクターと認識してしまうことがあるためです。

対処

add()の非同期代替関数を使ってクロージャーを使わないようにする

let request = UNNotificationRequest(identifier: "example", content: content, trigger: nil)
let center = UNUserNotificationCenter.current()
Task {
    try? await center.add(request)
    // 何らかの処理
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?