0

I've got a function in my code to check if we can have permission to display a UILocalNotification.

open func hasPermission() -> Bool {
    if let permissions = UIApplication.shared.currentUserNotificationSettings {
        if permissions.types.contains(.alert) {
            return true
        }
    }
    return false
}

This code has triggered a warning in Xcode 9:

UI API called from background thread: UIApplication.currentUserNotificationSettings must be used from main thread only

How do I fix this? I know there is the DispatchQueue.main.async method, just not sure how to implemented that.

Rool Paap
  • 1,918
  • 4
  • 32
  • 39

1 Answers1

1

You can do it like this.

DispatchQueue.main.async {
    if hasPermission() {
        //
    }
}
Bilal
  • 18,478
  • 8
  • 57
  • 72
  • This is part of a background job, so I'd prefer to do as little as possible on the main thread. This particular check is done as a guard hasPermission() else { – Rool Paap Oct 26 '17 at 08:20