gpt4 book ai didi

iOS 添加事件到日历很慢?

转载 作者:行者123 更新时间:2023-11-28 18:01:19 25 4
gpt4 key购买 nike

我已使用此代码将事件添加到 iphone 日历

-(void)setEvent:(NSString *)title withStartDate:(NSDate *)startDate withEndDate:(NSDate *)endDate completion:(void (^)(void))completionBlock
{

EKEventStore* store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted)
{
return;
}
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = title;
event.startDate = startDate;
event.endDate = endDate;
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
// NSString *savedEventId = event.eventIdentifier;
NSString* alertTitle;
NSString* msg;
if (err) {

alertTitle = @"Calendar was not set";
msg = @"Please set default calendar in settings.";
}
else
{
alertTitle = @"Event added";
msg = @"Event had been added in your calendar.";
}
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:alertTitle message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
completionBlock();

}];
}

但添加事件非常慢。我必须等待大约 10-12 秒才能完成此方法。这是正常现象还是我做错了什么?我已经搜索过相同但仍然没有运气。任何建议将不胜感激。

最佳答案

经过大约 2 天的研究,我找到了解决方案 - 我将事件保存代码放在 dispatch_async 中,这是我更新的添加事件的方法 -

-(void)setEvent:(NSString *)title withStartDate:(NSDate *)startDate withEndDate:(NSDate *)endDate withResecheduling:(BOOL)rescheduling completion:(void (^)(void))completionBlock
{

[[BusyIndicator sharedInstance] startIndicator];
EKEventStore* store = [[EKEventStore alloc] init];

[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted)
{
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = title;
event.startDate = startDate;
event.endDate = endDate;
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
// NSString *savedEventId = event.eventIdentifier;
if (!rescheduling) {

NSString* alertTitle;
NSString* msg;
if (err) {

alertTitle = @"Calendar was not set";
msg = @"Please set default calendar in settings.";
}
else
{
alertTitle = @"Event added";
msg = @"Event has been added in your calendar.";
}
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:alertTitle message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}

[[BusyIndicator sharedInstance] stopIndicator];
completionBlock();
});


}];
}

关于iOS 添加事件到日历很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19378704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com