Search code examples
iosswift3ekeventstore

Function execution delayed (after requesting for access permission) IOS


I need permission for using iOS's reminder and for that i'm doing this :

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")
}   

As explained above when app prompts user for permission of Reminder and user grants the permission my next Function got executed but after a while (5-10 seconds)

anyone can explain why is it happening ?


Solution

  • The completion of requestAccess doesn't get called on main thread. Put permissions granted code inside Dispatch Async:

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