【问题标题】:EKEventStore removeEvent EKErrorDomain Code=11 EKErrorObjectBelongsToDifferentStoreEKEventStore removeEvent EKErrorDomain 代码=11 EKErrorObjectBelongsToDifferentStore
【发布时间】:2012-05-30 07:22:13
【问题描述】:

我收到一个错误:

删除事件错误:Error Domain=EKErrorDomain Code=11“该事件不属于该事件存储。” UserInfo=0x1fdf96b0 {NSLocalizedDescription=该事件不属于该事件存储。

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

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

从我看到的所有示例中,我做的一切都是正确的。我还对 EKEventStore 的 eventStoreIdentifier 进行了 NSLog 处理,并且每次我在任何方法中访问它时它都是相同的,因此它应该是相同的 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];
}

【问题讨论】:

    标签: iphone ios ios5 eventkit ekevent


    【解决方案1】:

    发生这种情况是因为您是 always initializing a new instance of EKEventStore。当您将EKEvent 添加到EKEventStore 时,EKEventStore 的实例与您尝试删除时不同。你可以做的是在.h中声明EKEventStore引用变量并且只初始化一次。

    在.h -

    EKEventStore *eventStore;
    

    在.m -

    viewDidLoad内部-

    eventStore = [[EKEventStore alloc] init];
    

    然后从所有三个方法中删除这一行-

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

    【讨论】:

    • 成功了!谢谢!奇怪的是,我确实想到了这一点,但我也认为,因为 EKEventStore 的实例在应用程序关闭后不会持续存在,所以 eventIdentifier 的全部意义在于将来重新获取该事件。当我能够使用 eventIdentifier 从我的任何 EKEventStore 实例中获取事件时,这让我更加困惑。我不确定为什么有人想要超过 1 个 EKEventStore 实例,但对于大多数用途来说,创建这些事件并将这些事件获取到单例的类方法肯定是最好的方法。无论如何很高兴解决这个问题。干杯!
    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2013-06-24
    • 2017-09-22
    • 2016-07-30
    • 2016-05-07
    • 1970-01-01
    • 2013-10-03
    相关资源
    最近更新 更多