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

后端 未结 1 1161
慢半拍i
慢半拍i 2021-01-14 17:17

Part of my app requires calendar access, which requires a call to the EKEventStore method -(void)requestAccessToEntityType:(EKEntityType)entityType comple

相关标签:
1条回答
  • 2021-01-14 18:05

    [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];
                                       }
                                   });
                               }];
    
    0 讨论(0)
提交回复
热议问题