I'm trying to make a massive load of events in a calendar ios either locally or in gmail (The calendar is chosen by the user in the manner I describe in the following answer ) using objective-c .
Adding an event with the functions I have set up below works well for me, but when I have to do a massive load of for example 527 events (since I'm trying to add a student's school calendar) it does not work correctly.
When doing the massive load I insert well about 100 events more or less and then it starts to fail and the app is blocked.
The errors that it gives me are the following:
2016-11-17 17: 23: 35.966 [230: 11481] Calendar was not set: 1 Error Domain = EKErrorDomain Code = 1 "No calendar has been selected." UserInfo = {NSLocalizedDescription = None selected calendar.}
2016-11-17 17: 23: 49.545 [230: 12644] Connectioninterrupted!
2016-11-17 17: 23: 49.568 [230: 12587] Error getting changed object IDs since timestamp 501092601.149441 from daemon: Error Domain = NSMachErrorDomain Code = 4097 "unknown error code"
My question is this, is there any error in my bulk upload approach or in the functions I have done? or otherwise Is there any other better way to make a Massive load of events?
Function that runs through the list of events and inserts them:
- (int) anyadirEventosCalendario: (EKCalendar *) cal {
int num = 0;
//se añade cada uno de los eventos
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;
}
And the function that adds the event to the calendar is the following:
-(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];
//Obtenemos las fechas del evento
Fecha_DTO *fechaStart = [[Fecha_DTO alloc] initWithFecha:(NSDictionary *)evento.dtStart];
Fecha_DTO *fechaEnd = [[Fecha_DTO alloc] initWithFecha:(NSDictionary *)evento.dtEnd];
// Formateamos las fechas a tipo NSDate
// Fecha ini
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];
// Fecha fin
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];
//Obtenemos las 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:@""]) // sólo añadirmos rRule si hay
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(@"EVENTO AÑADIDO");
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(@"la fecha de inicio es nula");
}
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;
}