Post

Replies

Boosts

Views

Activity

Reply to NotificationCenter Crash On iOS 18+ Swift6.2
You were right. We've found out that the issue was that we registered an observer with addObserver using a #selector that pointed to a function marked as @MainActor, but we were calling the NotificationCenter.post method from a background thread. Apparently this caused the main-actor isolation check to fail, which led to a crash. Even though there is no data-race or UI operation in the function, looks like it is enough for the function to be @MainActor to cause a crash when called from outside the main thread. We fixed it by making sure the relevant NotificationCenter.post calls are executed on the main thread. Initially, we thought that marking the function with @MainActor would prevent it from being called from a nonisolated context by producing a compile-time error in Swift 6 mode. But since a Selector instance is not aware of the actor isolation of the target function, Xcode is not able to show a warning and it caused us to not realize the issue at first. I appreciate your guidance @DTS Engineer.
Topic: Programming Languages SubTopic: Swift Tags:
2w