在完成块中调用时,UIAlertView需要很长时间才能显示 [英] UIAlertView takes a long time to appear when called in a completion block

查看:145
本文介绍了在完成块中调用时,UIAlertView需要很长时间才能显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的部分应用需要日历访问权限,需要调用 EKEventStore 方法 - (void)requestAccessToEntityType:(EKEntityType)entityType completion: (EKEventStoreRequestAccessCompletionHandler)完成从iOS 7开始。

Part of my app requires calendar access, which requires a call to the EKEventStore method -(void)requestAccessToEntityType:(EKEntityType)entityType completion:(EKEventStoreRequestAccessCompletionHandler)completion as of iOS 7.

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

I added the request, and everything runs smoothly if the user selects to allow access, but a problem arises if the user denies or has previously denied access. I added a UIAlertView to notify the user if access is denied, but the UIAlertView consistently takes 20-30 seconds to appear, and completely disables the UI during that time. Debugging has shown that [alertView show] runs before the delay, even though it doesn't actually show until after the delay.

为什么会发生这种延迟?如何删除它?

Why is this delay happening and how can I remove it?

[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];
                               }
                           }];


推荐答案

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

[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];
                                   }
                               });
                           }];

这篇关于在完成块中调用时,UIAlertView需要很长时间才能显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆