gpt4 book ai didi

ios - 将事件添加到日历非常慢

转载 作者:技术小花猫 更新时间:2023-10-29 11:01:51 24 4
gpt4 key购买 nike

我只是想向设备的日历中添加一个事件。

我正在使用:

 __weak ProgramViewController *weakSelf = self;

EKEventStore *store = [[EKEventStore alloc] init];

[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (error)
NSLog(@"EKEventStore error = %@", error);

if (granted)
{
NSLog(@"EKEvent *event ");

EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = weakSelf.program.title;
event.location = weakSelf.program.locationPublic;
event.startDate = weakSelf.program.startTime;
event.endDate = weakSelf.program.endTime;
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

if (err)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Calendar Error" message:err.localizedDescription delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Added" message:@"Calendar event added." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
}];

在 iOS 6 中,它可能需要 6/7 秒 (iPhone 4),在 iOS 7 中(在 iPhone 5S 上)大约需要 10 秒。这是正常行为吗?如果不是,我做错了什么?

最佳答案

我遇到了同样的问题。感谢 Jasper 的回答,我开始考虑队列。试试这个:

    if (!err)
{
dispatch_async(dispatch_get_main_queue(),
^{
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"event added", nil) message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil] show];
});
}

这就是为什么需要这样做(见重点)

Discussion

In iOS 6 and later, requesting access to an event storeasynchronously prompts your users for permission to use their data.The user is only prompted the first time your app requests access toan entity type; any subsequent instantiations of EKEventStore usesexisting permissions. When the user taps to grant or deny access, thecompletion handler will be called on an arbitrary queue. Your app isnot blocked while the user decides to grant or deny permission.

由于 UIAlertView 是 UIKit,而 UIKit 总是需要主线程,任何其他任意线程都会崩溃或导致不可预测的行为。

https://developer.apple.com/library/ios/documentation/EventKit/Reference/EKEventStoreClassRef/Reference/Reference.html

关于ios - 将事件添加到日历非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19355770/

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