swift - 如何解决 EKEventStore 上 requestAccess(to:completion:) 中的无限循环?
问题描述
我正在打开,EKAuthorizationStatus
但即使在requestAuthorisation(to:commit:)
被调用并返回 true 并且没有错误之后 switch 语句仍然匹配.notDetermined
大小写,并且其中的递归正在产生无限循环。它让我发疯!
我试图找出requestAuthorisation(to:commit:)
实际是如何工作的,因为我觉得这个问题全都与并发或其他问题有关,但我找不到任何东西,所以我很难真正理解这种情况。
而且由于我的代码中的递归绝对是这个无限循环的一部分,我尝试了一种没有递归的方法。但是由于EKAuthorizationStatus
在我的应用程序对事件存储的调用之间可能会发生变化,所以我想事先检查它以相应地对它的所有状态做出反应。因此,我必须调用我的方法来切换授权状态,并调用一个方法来请求它,并处理整个班级的任何错误,出于可读性、安全性和理智的原因,我不想这样做。
private func confirmAuthorization(for entityType: EKEntityType) throws {
switch EKEventStore.authorizationStatus(for: entityType) {
case EKAuthorizationStatus.notDetermined:
// Request authorisation for the entity type.
requestAuthorisation(for: entityType)
// Switch again.
try confirmAuthorization(for: entityType)
case EKAuthorizationStatus.denied:
print("Access to the event store was denied.")
throw EventHelperError.authorisationDenied
case EKAuthorizationStatus.restricted:
print("Access to the event store was restricted.")
throw EventHelperError.authorisationRestricted
case EKAuthorizationStatus.authorized:
print("Acces to the event store granted.")
}
}
private func requestAuthorisation(for entityType: EKEntityType) {
store.requestAccess(to: entityType) { (granted, error) in
if (granted) && (error == nil) {
DispatchQueue.main.async {
print("User has granted access to \(String(describing: entityType))") // It's being printed over and over
}
} else {
DispatchQueue.main.async {
print("User has denied access to \(String(describing: entityType))")
}
}
}
}
我预计开关将与.notDetermined
第一次启动时的情况相匹配,它会请求授权。因此,当我再次切换状态时,它现在应该匹配不同的大小写,例如.authorized
or .denied
。但实际上它.notDetermined
再次匹配案例并且访问被一遍又一遍地授予。\ >:[
安慰:
>2019-01-08 12:50:51.314628+0100 EventManager[4452:190572] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
>2019-01-08 12:50:54.608391+0100 EventManager[4452:190572] Adding a new event.
>2019-01-08 12:50:54.784684+0100 EventManager[4452:190572] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/***/Library/Developer/CoreSimulator/Devices/********-****-****-****-************/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
>2019-01-08 12:50:54.785638+0100 EventManager[4452:190572] [MC] Reading from private effective user settings.
>Acces to the event store granted.
>Saved event with identifier: Optional("F8EAC467-9EC2-476C-BF30-45588240A8D0:903EF489-BB52-4A86-917B-DF72494DEA3D")
>2019-01-08 12:51:03.019751+0100 EventManager[4452:190572] Events succsessfully saved.
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>[…]
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>2019-01-08 12:51:03.291606+0100 EventManager[4452:190572] [Common] _BSMachError: port 26b03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
>2019-01-08 12:51:03.317800+0100 EventManager[4452:190572] [Common] _BSMachError: port 26b03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>[…]
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>Acces to the event store granted.
>Preset <EventManager.EventCreationPreset: 0x6000020ca340> needs update.
>Acces to the event store granted.
>Preset <EventManager.EventCreationPreset: 0x6000020ca340> was updated.
>2019-01-08 12:51:03.567071+0100 EventManager[4452:190572] Events succsessfully saved.
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>[…]
解决方案
requestAuthorisation
异步运行,因此在confirmAuthorization
授权对话框呈现给用户之前再次被调用。
通常,在这种模式中(希望以异步模式递归调用某些东西),解决方案是将递归调用移动到异步方法的完成处理程序中。但是在这种情况下,用户得到授权对话框后,要么接受要么拒绝,不必担心“如果还没有确定怎么办”的状态。因此,最重要的是,在这种情况下不需要或不需要递归。
话虽如此,您显然确实希望将状态返回给调用者。但是错误抛出模式不起作用,因为您需要处理异步情况(未确定权限并且我们需要显示确认对话框)。
因此,我建议您始终使用完成处理程序模式:
private func confirmAuthorization(for entityType: EKEntityType, completion: @escaping (EKAuthorizationStatus) -> Void) {
let status = EKEventStore.authorizationStatus(for: entityType)
switch status {
case .notDetermined:
requestAuthorisation(for: entityType, completion: completion)
default:
completion(status)
}
}
private func requestAuthorisation(for entityType: EKEntityType, completion: @escaping (EKAuthorizationStatus) -> Void) {
store.requestAccess(to: entityType) { _, _ in
DispatchQueue.main.async {
completion(EKEventStore.authorizationStatus(for: entityType))
}
}
}
或者,您可以将其简化为一种方法:
private func confirmAuthorization(for entityType: EKEntityType, completion: @escaping (EKAuthorizationStatus) -> Void) {
let status = EKEventStore.authorizationStatus(for: entityType)
switch status {
case .notDetermined:
store.requestAccess(to: entityType) { _, _ in
DispatchQueue.main.async {
completion(EKEventStore.authorizationStatus(for: entityType))
}
}
default:
completion(status)
}
}
那么你就可以:
confirmAuthorization(for: .event) { status in
switch status {
case .authorized:
// proceed
default:
// handle non-authorized process here
}
}
// But, remember, the above runs asynchronously, so do *not*
// put any code contingent upon the auth status here. You
// must put code contingent upon authorization inside the above
// completion handler closure.
//
推荐阅读
- java - 应用程序没有从编辑文本中获得价值,似乎在跳行
- elasticsearch - 根据匹配的数量应用越来越慢的查询过滤器
- python - IndexError:使用正则表达式列出超出范围的索引
- javascript - Angular 7 地图运算符不适用于 httpClient Observable
- arrays - MQL5:如何对数组的值进行排名
- javascript - 如何在右中心对齐 Chart JS“图例”
- batch-file - 如何修复“'rm'不是内部或外部命令、可运行程序或批处理文件。” cmd中的错误
- sql - sql - 为具有最低值的每个组选择单个 ID
- c# - Topshelf NLog - Windows 服务(控制台应用程序)未按预期工作
- python - WAMP 服务器在 DEBUG = True 时加载静态文件,而不是在 DEBUG=False 时加载静态文件,即使在 collectstatic 之后也是如此