ios - 阻止卡住我的应用程序

标签 ios objective-c iphone objective-c-blocks

我的应用程序有一个小问题。

当我使用我的 block 时,应用程序卡住了很长时间(几乎 1 分钟),因此我们无能为力,所有滚动/按钮/等都不起作用。

我想优化我的代码,使其运行得更快,并且用户不必等待 1 分钟

- (IBAction)exporter:(id)sender {
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Accès aux calendrier" message:@"L'accès au calendrier est nécessaire pour utiliser cette fonctionnalité" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    }


    EKEvent *event = [EKEvent eventWithEventStore:store];

    Variables *objStatutCompte = [Variables getStatutCompte];

    if([objStatutCompte.statutCompte isEqualToString:@"clt"])
    {
        NSString* titreEvent = [NSString stringWithFormat:@"Intervention avec %@", intervenantRecup];
        event.title=titreEvent;

    }
    else if([objStatutCompte.statutCompte isEqualToString:@"slr"])
    {
        NSString* titreEvent = [NSString stringWithFormat:@"Intervention chez %@", clientRecup];
        event.title=titreEvent;
    }

    NSString* lieuEvent = [NSString stringWithFormat:@"%@, %@", adresseClientRecup, villeRecup];


    event.location=lieuEvent;

    NSString* currentDay = [dateRecup substringWithRange:NSMakeRange(0,2)];
    NSInteger jourCourant = [currentDay integerValue];

    NSString* currentMonth = [dateRecup substringWithRange:NSMakeRange(3,2)];
    NSInteger moisCourant = [currentMonth integerValue];

    NSString* currentYear = [dateRecup substringWithRange:NSMakeRange(6,4)];
    NSInteger anneeCourante = [currentYear integerValue];

    NSString* dateDebut = [NSString stringWithFormat:@"%d-%d-%02d %@:00", anneeCourante, moisCourant, jourCourant, heureDebutRecup];
    NSString* dateFin = [NSString stringWithFormat:@"%d-%d-%02d %@:00", anneeCourante, moisCourant, jourCourant, heureFinRecup];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setTimeZone:nil];
    NSDate* dateD = [dateFormatter dateFromString:dateDebut];

    NSDate* dateF = [dateFormatter dateFromString:dateFin];

    event.startDate=dateD;
    event.endDate=dateF;

    NSMutableArray *myAlarmsArray = [[NSMutableArray alloc] init];
    EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-3600]; // 1 Hour
    [myAlarmsArray addObject:alarm1];

    event.alarms=myAlarmsArray;

    [event setCalendar:[store defaultCalendarForNewEvents]];
    NSError *err = nil;
    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Evénement ajouté"
                                                    message:@"L'évenement a bien été ajouté"
                                                   delegate:self
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles:nil, nil];
    [alert show];

}];

}

最佳答案

requestAccessToEntityType 的文档说:

When the user taps to grant or deny access, the completion handler will be called on an arbitrary queue. Your app is not blocked while the user decides to grant or deny permission.

关键在于,completionHandler 可能不会在主队列上调用,但所有 UI 更新都必须在主线程上进行。

因此,您应该将 UI 代码分派(dispatch)到主队列:

[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
    dispatch_async(dispatch_get_main_queue(), ^{
        // your code here
    });
}];

关于ios - 阻止卡住我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27381047/

相关文章:

iphone - 如何在 iPhone 上读取大型 UTF-8 文件?

iphone - 寻找有关 Apple MFi 计划注册流程的经验

objective-c - UITableViewCell 不可选择,而它的accessoryView 可选择

ios - 使用 SwiftUI 如何强制 DatePicker 使用 24 小时日期格式而不是用户设备上的区域设置

ios - 根据 NSString 日期对对象进行排序

ios - 1 < -1 的计算结果为 TRUE

objective-c - 更改 UITableViewCell 中 UITableViewCellDeleteConfirmationControl(删除按钮)的颜色

javascript - 如何在 iOS 中获取 js 文件路径?

iphone - 哪个选项给我更好的性能 : a variable declaration or an implicit function in iOS

ios - 应用程序的TestFlight分发中的到期日期