With Xcode 15.1, this code
func test1() async {
let _ = await NotificationCenter.default.notifications(named: UIApplication.didBecomeActiveNotification)
return
}
would compile fine, but in Xcode 16, the same code will give warning "No async operations occur within await expression".
The function signature is
public func notifications(named name: Notification.Name, object: AnyObject? = nil) -> NotificationCenter.Notifications
which isn't a async function. I just wonder why in Xcode 15 it needs await while in Xcode 16 it doesn't?
Maybe it has something to do with changes with actor? I noticed that if I mark test1 with @MainActor, both Xcode 15 and 16 don't require me to use await. Is it because NotificationCenter.default.notifications now runs on MainActor by default?
NeonTetra
(Park Byeong Gwan)
2
The reason is didBecomeActiveNotification is bounded to UIApplication which is MainActor isolated class.
Declaration is different from swift 6 and before it
this is swift 6 declaration
extension UIApplication {
nonisolated public class let willEnterForegroundNotification: NSNotification.Name
nonisolated public class let didFinishLaunchingNotification: NSNotification.Name
nonisolated public class let didBecomeActiveNotification: NSNotification.Name
nonisolated public class let willResignActiveNotification: NSNotification.Name
nonisolated public class let didReceiveMemoryWarningNotification: NSNotification.Name
}
and this is the original declaration
extension UIApplication {
public class let willEnterForegroundNotification: NSNotification.Name
public class let didFinishLaunchingNotification: NSNotification.Name
public class let didBecomeActiveNotification: NSNotification.Name
public class let willResignActiveNotification: NSNotification.Name
public class let didReceiveMemoryWarningNotification: NSNotification.Name
}
So before swift 6 these global variable was accidentally bounded to MainActor. And these is the reason why await kicks in.
2 Likes