我的应用程序有一个小问题。
当我使用我的 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/