I have the following code giving me the following error message:
class Variables: ObservableObject {
@Published var isNotificationsEnabled: Bool = false
}
@EnvironmentObject var v: Variables
func notificationsEnabled() {
let current = UNUserNotificationCenter.current()
current.getNotificationSettings(completionHandler: { (settings) in
if settings.authorizationStatus == .notDetermined {
v.isNotificationsEnabled = false
} else if settings.authorizationStatus == .denied {
// Notification permission was previously denied, go to settings & privacy to re-enable
v.isNotificationsEnabled = false
} else if settings.authorizationStatus == .authorized {
// Notification permission was already granted
v.isNotificationsEnabled = true
}
})
}
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.
What can I do to fix this? I can't change the published variable
CodePudding user response:
Wrap the if
in a DispatchQueue.main.async