【发布时间】:2016-11-18 08:57:56
【问题描述】:
我正在尝试使用 @987654323 在 ios 本地或 gmail 日历上进行大量加载事件(日历选择用户,正如我在下面的 answer 中描述的那样) @。
使用我在下面放置的功能添加一个事件对我来说很好,但是当我必须做大量的事件时,例如 527 个事件(因为我试图添加学生的学校日历)它不能正常工作.
在进行批量加载时,我很好地插入了大约 100 个左右的事件,然后它开始崩溃并使应用程序崩溃。
它给我的错误如下:
2016-11-17 17:23:35.966 [230:11481] 日历未设置:1 错误 Domain=EKErrorDomain Code=1 "未选择日历。" UserInfo={NSLocalizedDescription=未选择日历。}
2016-11-17 17:23:49.545 [230:12644] 连接中断!
2016-11-17 17:23:49.568[230:12587] 获取更改的对象 ID 时出错 由于来自守护进程的时间戳 501092601.149441:错误 Domain=NSMachErrorDomain Code=4097 "未知错误代码"
我的问题是这样的:我的大量加载方法或我所做的功能是否有任何错误? 或者 还有其他更好的方法来处理大量事件?
插入事件列表的函数:
- (int) addCalendarEvents: (EKCalendar *) cal {
int num = 0;
for (int i=0; i < [calendario.eventos count]; i++) {
NSDictionary * nextDict = [calendario.eventos objectAtIndex:i];
Evento_DTO * evento_dto = [[Evento_DTO alloc] initWithEventos:nextDict];
BOOL res = [self addEventCalendar: evento_dto calendar: cal];
if(res){
num++;
}
}
return num;
}
并将事件添加到日历的函数如下:
-(BOOL)addEventCalendar: (Evento_DTO *) evento calendar: (EKCalendar *) cal{
__block BOOL res = NO;
if (!SYSTEM_VERSION_LESS_THAN(@"6.0")) {
// iOS 6 and later
EKEventStore *eventStore = [[EKEventStore alloc] init];
//We get the dates of the event
Fecha_DTO *fechaStart = [[Fecha_DTO alloc] initWithFecha:(NSDictionary *)evento.dtStart];
Fecha_DTO *fechaEnd = [[Fecha_DTO alloc] initWithFecha:(NSDictionary *)evento.dtEnd];
// Format the dates to type NSDate
// Start Date
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyyMMdd'T'HHmmss"];
if (fechaStart.tzid == nil) {
[df setTimeZone: [NSTimeZone systemTimeZone]];
}else{
[df setTimeZone:[NSTimeZone timeZoneWithName:fechaStart.tzid]];
}
NSDate* parsedDateS = [df dateFromString: fechaStart.fecha];
// End Date
NSDateFormatter* df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:@"yyyyMMdd'T'HHmmss"];
if (fechaEnd.tzid == nil) {
[df2 setTimeZone: [NSTimeZone systemTimeZone]];
}else{
[df2 setTimeZone:[NSTimeZone timeZoneWithName:fechaEnd.tzid]];
}
NSDate* parsedDateE = [df2 dateFromString: fechaEnd.fecha];
//rRules
NSString *rfc2445String = evento.rRule; // Usando la libreria EKRecurrenceRule+RRULE.m
EKRecurrenceRule *recurrenceRule;
if (![rfc2445String isEqualToString:@""]) {
recurrenceRule = [[EKRecurrenceRule alloc] initWithString:rfc2445String andTimezone:fechaStart.tzid];
// NSLog(@"RRule: %@", recurrenceRule);
}
if(parsedDateS!=nil){
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = evento.summary;
event.notes = evento.description;
event.startDate = parsedDateS;
event.endDate = parsedDateE;
event.location = evento.location;
if (![rfc2445String isEqualToString:@""])
event.recurrenceRules = [NSArray arrayWithObject:recurrenceRule];
event.calendar = [eventStore calendarWithIdentifier: cal.calendarIdentifier];
//[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err = nil;
BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
if(!success){
if (err) {
NSLog(@"Calendar was not set: %li %@", (long)err.code, err.description);
}
}else{
//NSLog(@"Added Event");
res = YES;
}
} else {
// code here for when the user does NOT allow your app to access the calendar
alerta = [[UIAlertView alloc]initWithTitle:AMLocalizedString(@"Error", @"")
message:AMLocalizedString(@"errorPermisosCal", @"")
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alerta show];
}
}];
}else{
NSLog(@"The start date is null");
}
df = nil;
df2 = nil;
}else{
alerta = [[UIAlertView alloc]initWithTitle:AMLocalizedString(@"Error", @"")
message:AMLocalizedString(@"VersionEvento", @"")
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alerta show];
}
return res;
}
【问题讨论】:
-
我们最近在一个大型事件集中发现了这种现象——有 3 个事件没有 eventIdentifiers。这给我们的代码带来了问题,但我们通过检查 nil 并了解对我们应用程序的影响来解决它。
-
@AndyWeinstein 我终于能够毫无错误地进行大量批量处理,看看我的回答
标签: ios objective-c ios objective-c ekevent ekeventstore ekeventkit