iphone - EKEventStore removeEvent EKErrorDomain 代码=11 EKErrorObjectBelongsToDifferentStore

标签 iphone ios ios5 eventkit ekevent

我收到一个错误:

删除事件错误: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];
}

最佳答案

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

在 .h -

EKEventStore *eventStore;

在.m-

viewDidLoad 中 -

eventStore = [[EKEventStore alloc] init];

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

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

关于iphone - EKEventStore removeEvent EKErrorDomain 代码=11 EKErrorObjectBelongsToDifferentStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10811726/

相关文章:

iphone - 当我的应用程序在后台播放音频时,如何用我的应用程序图标替换 iPod?

iphone - objective-c 中的后台任务

ios - UICollectionView,如何防止移动一个单元格

ios - 使用密码从URL提取数据

ios5 - 在 Open GL ES 2.0 (iOS GLKit) 中处理多个网格?

ios - Xcode 7 中 ios 8.0 之前的自动首选最大布局宽度

iphone - 用于 iPhone 到 Mac 集成的 CFNetwork 和 Bonjour 集成

ios - 如何获取自定义 UIPickerView?

ios - 从 AppDelegate 获取数据到 iOS 中的 ViewController

iphone - 即使安装了 iOS 5 模拟器也无法测试