Som*_*per 2 completionhandler swift
我知道这不行,因为它completion handler是在一个background Thread但是
我应该在哪里派遣主队列或我还需要做什么?
这是代码:
static func isNotificationNotDetermined() -> Bool{
var isNotDetermined = false
UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
switch notificationSettings.authorizationStatus {
case .notDetermined:
isNotDetermined = true
case .authorized:
isNotDetermined = false
case .denied:
isNotDetermined = false
}
}
return isNotDetermined
}
Run Code Online (Sandbox Code Playgroud)
你不能这样做; getNotificationSettings是异步的,所以你应该在方法中传递一个闭包,并在切换后立即调用.像这样的东西:
static func isNotificationNotDetermined(completion: (Bool) -> Void) {
UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
var isNotDetermined = false
switch notificationSettings.authorizationStatus {
case .notDetermined:
isNotDetermined = true
case .authorized:
isNotDetermined = false
case .denied:
isNotDetermined = false
}
// call the completion and pass the result as parameter
completion(isNotDetermined)
}
}
Run Code Online (Sandbox Code Playgroud)
然后你将这样调用这个方法:
YourClass.isNotificationNotDetermined { isNotDetermined in
// do your stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1376 次 |
| 最近记录: |