函数执行延迟(请求访问权限后)IOS

Function execution delayed (after requesting for access permission) IOS

我需要获得使用 iOS 提醒的权限,为此我正在这样做:

switch EKEventStore.authorizationStatus(for: .reminder) {
case .authorized:                    
    print("Access granted")
    //everything's normal here
    //executing my function here

case .denied:
    print("Access denied")
case .notDetermined:       
    print("not defined yet")

    //No determined so asking for permission
    self.eventStore.requestAccess(to: .reminder) { (granted, error) -> Void in
        if granted == true {
            print("permission granted")

           //executing my function here after getting permissions but this piece of code executes after a long delay 
           //this piece of codes are executing after a while say 5-10 seconds

        }else if error != nil{       
            print("ther's an error : \(error)")
        }            
    }

default:
    print("Case Default")
}   

如上所述,当应用程序提示用户获得提醒权限并且用户授予权限时,我的下一个功能得到执行但过了一段时间(5-10 秒)

谁能解释为什么会这样?

请求权限纯粹是一个异步过程,您不能立即执行该功能,因为我们的代码无法控制它。应用程序代码可以请求权限,当 OS 授予权限时,我们会获得委托回调处理程序,基于此,实际接收请求的权限存在延迟。

也有可能您正在请求 thread/block 的权限,而不是 UI 主线程上的 运行,并且在执行该代码时会有不可见的延迟。您必须检查启动权限请求的代码。

没有在主线程上调用 requestAccess 的完成。将 permissions granted 代码放入 Dispatch Async:

DispatchQueue.main.async {
  print("permission granted")
}