gpt4 book ai didi

swift - 如何解决 requestAccess 中的无限循环(到 :completion:) on EKEventStore?

转载 作者:搜寻专家 更新时间:2023-10-30 22:10:06 29 4
gpt4 key购买 nike

我正在打开 EKAuthorizationStatus,但即使在 requestAuthorisation(to:commit:) 被调用并返回 true 并且没有错误时,switch 语句仍然与 匹配。 notDetermined 情况和其中的递归产生无限循环。这让我抓狂!

我试图找出 requestAuthorisation(to:commit:) 实际上是如何工作的,因为我觉得这个问题完全与并发性或其他问题有关,但我找不到任何东西,所以我有麻烦真正了解情况。

而且由于我代码中的递归肯定是这个无限循环的一部分,所以我尝试了一种没有递归的方法。但是由于 EKAuthorizationStatus 可能会在我的应用程序对事件存储的调用之间发生变化,我想事先检查它以相应地对所有状态使用react。因此,我将不得不调用我的方法来切换授权状态和请求授权状态的方法,并处理整个类中的任何错误,出于可读性、安全性和健全性的原因,我不想这样做。

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.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
>[…]

最佳答案

requestAuthorization 异步运行,因此 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.
//

关于swift - 如何解决 requestAccess 中的无限循环(到 :completion:) on EKEventStore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54094914/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com