madhavakintek
madhavakintek

Reputation: 21

EKEventStore removeEvent EKErrorDomain Code=11 EKErrorObjectBelongsToDifferentStore

I am getting an error:

removing event error: Error Domain=EKErrorDomain Code=11 "That event does not belong to that event store." UserInfo=0x1fdf96b0 {NSLocalizedDescription=That event does not belong to that event store.

When I try to remove an EKEvent I just created.

The code below shows that I am storing the eventIdentifier and using it to retrieve the event. Furthermore when I do this, and NSLog the event I can see all the properties of it correctly.

From all the examples I have seen I am doing everything correctly. I also NSLog'ed the EKEventStore's eventStoreIdentifier and its the same every time I access it in any of my methods so it should be the same EKEventStore.

Any help would be greatly appreciated.

- (EKEvent *) getCurrentCalendarEvent {
    NSString *currentCalendarEventID = [[UserModel sharedManager] currentCalendarEventID];
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *event = [eventStore eventWithIdentifier:currentCalendarEventID];
    return event;
}

- (void) removeCurrentCalendarEvent {
    EKEvent *event = [self getCurrentCalendarEvent];
    if (event) {
        NSError *error;
        EKEventStore *eventStore = [[EKEventStore alloc] init];
        [eventStore removeEvent:event span:EKSpanFutureEvents error:&error];
    }
}

- (void) addCurrentCalendarEvent {
    [self removeCurrentCalendarEvent];

    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
    event.title = [[UserModel sharedManager] reminderLabel];
    event.notes = @"Notes";

    NSDate *startDate = [NSDate date];
    int futureDateSecs = 60 * 5;
    NSDate *endDate = [startDate dateByAddingTimeInterval:futureDateSecs];

    event.startDate = startDate;
    event.endDate = endDate;

    [event setCalendar:[eventStore defaultCalendarForNewEvents]];

    NSError *error;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&error];

    [[UserModel sharedManager] setCurrentCalendarEventID:event.eventIdentifier];
    [[UserModel sharedManager] saveToDefaults];
}

Upvotes: 2

Views: 2408

Answers (1)

saadnib
saadnib

Reputation: 11145

This is happening because you are always initializing a new instance of EKEventStore. When you are adding EKEvent to EKEventStore then the instance of EKEventStore is different then when you are trying to remove. What you can do is that declare EKEventStore reference variable in .h and initialize it only one time.

in .h -

EKEventStore *eventStore;

in .m -

inside viewDidLoad -

eventStore = [[EKEventStore alloc] init];

then remove this line from all of three methods-

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

Upvotes: 7

Related Questions