EKEventStore removeEvent EKErrorDomain Code = 11 EKErrorObjectBelongsToDifferentStore

mad*_*tek 2 iphone ios eventkit ios5 ekevent

我收到一个错误:

删除事件错误:错误Domain = EKErrorDomain Code = 11"该事件不属于该事件存储." UserInfo = 0x1fdf96b0 {NSLocalizedDescription =该事件不属于该事件存储.

当我尝试删除刚创建的EKEvent时.

下面的代码显示我正在存储eventIdentifier并使用它来检索事件.此外,当我这样做,NSLog事件,我可以正确地看到它的所有属性.

从我看到的所有例子中我都正确地做了一切.我也是NSLog'ed EKEventStore的eventStoreIdentifier,每当我在任何一个方法中访问它时它都是一样的,所以它应该是相同的EKEventStore.

任何帮助将不胜感激.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

saa*_*nib 6

发生这种情况是因为你是always initializing a new instance of EKEventStore.当你添加EKEventEKEventStore那时,实例EKEventStore是不同的,然后当你试图删除.您可以做的是EKEventStore在.h 中声明引用变量并仅初始化它一次.

in .h -

EKEventStore *eventStore;
Run Code Online (Sandbox Code Playgroud)

在.m -

里面viewDidLoad-

eventStore = [[EKEventStore alloc] init];
Run Code Online (Sandbox Code Playgroud)

然后从三种方法中删除这一行 -

EKEventStore *eventStore = [[EKEventStore alloc] init];
Run Code Online (Sandbox Code Playgroud)

  • 有效!谢谢!奇怪的是,我确实想到了,但我也认为,因为一旦应用程序关闭,EKEventStore的实例就不会持续存在,eventIdentifier的重点是将来重新获取该事件.当我能够使用eventIdentifier从我的任何EKEventStore实例中获取事件时,这让我更加困惑.我不确定为什么有人想要超过1个EKEventStore实例,但是对于大多数用途来说,创建和将这些事件提取到单例的类方法肯定是最好的方法.无论如何,很高兴解决这个问题.干杯! (2认同)