Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 删除和重新创建日历_Ios_Calendar - Fatal编程技术网

Ios 删除和重新创建日历

Ios 删除和重新创建日历,ios,calendar,Ios,Calendar,在我的应用程序中,我在日历中创建事件。我在线获取事件,为了刷新事件,我想首先删除日历(如果存在),重新创建日历,然后将新事件放入其中 要实例化我使用的日历 - (EKCalendar *)calendar { if (!_calendar) { NSArray *calendars = [self.store calendarsForEntityType:EKEntityTypeEvent]; NSString *calendarTitle = @"MyC

在我的应用程序中,我在日历中创建事件。我在线获取事件,为了刷新事件,我想首先删除日历(如果存在),重新创建日历,然后将新事件放入其中

要实例化我使用的日历

- (EKCalendar *)calendar {
    if (!_calendar) {
        NSArray *calendars = [self.store calendarsForEntityType:EKEntityTypeEvent];
        NSString *calendarTitle = @"MyCalendar";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title matches %@", calendarTitle];
        NSArray *filtered = [calendars filteredArrayUsingPredicate:predicate];

        if ([filtered count]) {
            _calendar = [filtered firstObject];
        } else {
            _calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.store];
            _calendar.title = calendarTitle;
            _calendar.source = self.store.defaultCalendarForNewEvents.source;
            NSError *calendarErr = nil;
            BOOL calendarSuccess = [self.store saveCalendar:_calendar commit:YES error:&calendarErr];
            if (!calendarSuccess) {
                NSLog(@"Calendar Error = %@", [calendarErr localizedDescription]);
            }
        }
    }
    return _calendar;
}
要删除日历,我使用

-(IBAction)deleteCalendar{
    NSError *error = nil;
    [self.store removeCalendar:_calendar commit:YES error:&error];

}
这两种方法各自都能很好地工作。 因此,当我开始创建事件时,我会执行以下操作:

[self deleteCalendar];//delete calendar and its events, in case it already exists
[self calendar];//create calendar
[self importEvents];//put events in calendar
现在,我观察到如下情况:

[self deleteCalendar];//delete calendar and its events, in case it already exists
[self calendar];//create calendar
[self importEvents];//put events in calendar
在应用程序的第一次运行时

  • 将创建一个日历
  • 事件被导入。(这是意料之中的,而且效果很好)
当应用程序运行时,我用一个按钮再次触发上述方法。以下是令我困惑的结果:

  • 日历已删除(预期结果)
  • 没有创建日历(为什么?这是我的主要问题。)。该方法的“if(!\u calendar)”部分被认为是错误的,不会执行任何操作
  • “importEvents”方法贯穿其常规框架,没有任何明显的错误,尽管我希望出现类似“无源”的错误
请告知

更新:

这可能是正在发生的事情的一个指标,但我仍然不明白:

一段时间后,事件出现在另一个日历中,即不是名为“myCalendar”的日历,而是另一个基于iCloud的日历,显然,此时是新事件的默认日历。但是,这对我来说也没有任何意义。

好的,那么,发生了什么: 我已经有效地从商店中删除了日历,但在我的应用程序中仍然挂着对该日历的引用。 我的解决办法如下:

-(IBAction)deleteCalendar:(id)sender{
    NSError *error = nil;
    if(_calendar){
        [self.store removeCalendar:_calendar commit:YES error:&error];
    }
    _calendar = nil;
}
我希望这对某人有用