Ber*_*ard 6 recursion infinite-loop eventkit ekeventstore swift
我正在打开,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))")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我预计交换机将与.notDetermined第一次启动时的情况相匹配,它会请求授权.因此,当我再次切换状态时,它现在应该匹配不同的情况,如.authorized或.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
>[…]
Run Code Online (Sandbox Code Playgroud)
在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))
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以将其减少为单个方法:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以:
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.
//
Run Code Online (Sandbox Code Playgroud)