Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在swift中检查主线程上的通知权限?_Swift_Multithreading_Xcode9 - Fatal编程技术网

如何在swift中检查主线程上的通知权限?

如何在swift中检查主线程上的通知权限?,swift,multithreading,xcode9,Swift,Multithreading,Xcode9,我的代码中有一个函数,用于检查我们是否有权显示UILocalNotification open func hasPermission() -> Bool { if let permissions = UIApplication.shared.currentUserNotificationSettings { if permissions.types.contains(.alert) { return true } }

我的代码中有一个函数,用于检查我们是否有权显示UILocalNotification

open func hasPermission() -> Bool {
    if let permissions = UIApplication.shared.currentUserNotificationSettings {
        if permissions.types.contains(.alert) {
            return true
        }
    }
    return false
}
此代码在Xcode 9中触发了警告:

从后台线程调用的UI API: UIApplication.currentUserNotificationSettings必须从main中使用 仅螺纹


我该如何解决这个问题?我知道有DispatchQueue.main.async方法,只是不知道如何实现它。

您可以这样做

DispatchQueue.main.async {
    if hasPermission() {
        //
    }
}

这是后台工作的一部分,所以我更喜欢在主线程上尽可能少地工作。这个特殊的检查是作为一个警卫来完成的{