magnuskahr magnuskahr - 4 months ago 17
iOS Question

Eventkit doesn't always delete events

I am working on an app, that gets a student's school lessons online and saves it to the iPhone's calendar.

Every time the lessons get updated, I want to delete all events from the calendar of the week, and then put in the updated lessons for the whole week.

I got no problems in adding the events, but sometimes the events don't get removed?

dispatch_queue_t queue = dispatch_queue_create("com.xxxxr.xxxxxx.calendar", NULL);
dispatch_async(queue, ^{
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

if (granted){

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
EKCalendar *calendarIdentifier;

if ([defaults objectForKey:@"Calendar"] == nil || ![eventStore calendarWithIdentifier:[defaults objectForKey:@"Calendar"]]){
// Create Calendar if Needed

EKSource *theSource = nil;
for (EKSource *source in eventStore.sources) {
if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"]) {
theSource = source;
NSLog(@"iCloud Store Source");
break;
} else {
for (EKSource *source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal) {
theSource = source;
NSLog(@"iPhone Local Store Source");
break;
}
}
}
}

EKCalendar *cal;
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
cal.title = @"xxxxxxx";
cal.source = theSource;
[eventStore saveCalendar:cal commit:YES error:nil];
NSString *calendar_id = cal.calendarIdentifier;
[defaults setObject:calendar_id forKey:@"Calendar"];
calendarIdentifier = cal;

} else {
calendarIdentifier = [eventStore calendarWithIdentifier:[defaults objectForKey:@"Calendar"]];
NSLog(@"Calendar Existed");
}


/* NOW TO WHERE THE PROBLEM LIES! */
/* FIRST DELETE ALL EVENTS OF THIS WEEK */

NSArray *arrayOfIdentitiesToDelete = [defaults objectForKey:@"eventIdentities"];
for(NSString *identifierOfEventToDelete in arrayOfIdentitiesToDelete){
EKEvent *eventToRemove = [eventStore eventWithIdentifier:identifierOfEventToDelete];
NSError *error;
[eventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];
}

/* Then create new events from my 'arrayOfLessons' array */

NSMutableArray *arrayOfEventIdentities = [[NSMutableArray alloc] init];

for(int dayAddingToCalendar = 0; dayAddingToCalendar < 5; dayAddingToCalendar++){
for(NSArray *arrayOfDayAddingToCalendar in [[arrayOfLessons objectAtIndex:dayAddingToCalendar] objectAtIndex:3]){

EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.calendar = calendarIdentifier;
event.title = [arrayOfDayAddingToCalendar objectAtIndex:4];
event.location = [arrayOfDayAddingToCalendar objectAtIndex:1];
event.notes = [arrayOfDayAddingToCalendar objectAtIndex:0];
event.startDate = [arrayOfDayAddingToCalendar objectAtIndex:12];
event.endDate = [arrayOfDayAddingToCalendar objectAtIndex:13];
event.allDay = NO;

[eventStore saveEvent:event span:EKSpanThisEvent error:nil];
[arrayOfEventIdentities addObject:event.eventIdentifier];
}
}

[defaults setObject:arrayOfEventIdentities forKey:@"eventIdentities"];

} else {
NSLog(@"Not Granted");
}
}];
});

Answer

You are saying:

NSError *error;
[eventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];

So you are throwing away error-checking. Don't. Look at the error! That's what it's for. Like this:

NSError *error;
BOOL ok = [eventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];
if (!ok)
    NSLog(@"%@", error);