UIAlertView takes a long time to appear when called in a completion block

被刻印的时光 ゝ 提交于 2019-12-01 09:08:28

[alertView show] is not thread safe, so it is adding its UI change to the queue from which the completion block was dispatched rather than the main queue. I resolved this issue by adding dispatch_async(dispatch_get_main_queue(), ^{}); around the code inside the completion block:

[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];
                                   }
                               });
                           }];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!