3

我的应用程序的一部分需要日历访问,这需要从 iOS 7 开始调用该EKEventStore方法-(void)requestAccessToEntityType:(EKEntityType)entityType completion:(EKEventStoreRequestAccessCompletionHandler)completion

我添加了请求,如果用户选择允许访问,一切都会顺利进行,但是如果用户拒绝或之前拒绝访问,则会出现问题。如果访问被拒绝,我添加了一个UIAlertView通知用户,但UIAlertView始终需要 20-30 秒才能出现,并在此期间完全禁用 UI。调试表明它[alertView show]在延迟之前运行,即使它实际上直到延迟之后才显示。

为什么会发生这种延迟,我该如何消除它?

[eventStore requestAccessToEntityType:EKEntityTypeEvent
                           completion:^(BOOL granted, NSError *error) {
                               if (granted) {
                                   [self createCalendarEvent];
                               } else {
                                   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Calendar Access Denied"
                                                                                       message:@"Please enable access in Privacy Settings to use this feature."
                                                                                      delegate:nil
                                                                             cancelButtonTitle:@"OK"
                                                                             otherButtonTitles:nil];
                                   [alertView show];
                               }
                           }];
4

1 回答 1

12

[alertView show]不是线程安全的,因此它将其 UI 更改添加到调度完成块的队列而不是主队列。我通过dispatch_async(dispatch_get_main_queue(), ^{});在完成块内添加代码解决了这个问题:

[eventStore requestAccessToEntityType:EKEntityTypeEvent
                           completion:^(BOOL granted, NSError *error) {
                               dispatch_async(dispatch_get_main_queue(), ^{
                                   if (granted) {
                                       [self createCalendarEvent];
                                   } else {
                                       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Calendar Access Denied"
                                                                                           message:@"Please enable access in Privacy Settings to use this feature."
                                                                                          delegate:nil
                                                                                 cancelButtonTitle:@"OK"
                                                                                 otherButtonTitles:nil];
                                       [alertView show];
                                   }
                               });
                           }];
于 2014-03-11T18:31:41.480 回答