EKEventStore获取事件返回空列表

我想从我的应用程序中的特定日历中获取所有事件。 我在我的应用程序中创build了日历和testing事件(需要iOS 5.0或更高版本才能创build自定义日历)。 如果我在我的设备上运行应用程序,然后检查系统日历应用程序,然后我的日历和我创build的事件显示正确。

现在我想让我的应用程序读取这个自定义日历中的所有事件。 我的事件创build与startDate和endDate设置为NOW(NSDate alloced给没有timeInterval)。

NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:- 60 * 60 * 24 * (365 * 4 + 1)]; NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * 365 * 25]; NSArray *calendarList = [NSArray arrayWithObjects:tmpCal, nil]; NSPredicate *datePredicate = [store predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarList]; NSArray *eventList = [store eventsMatchingPredicate:datePredicate]; 

正如你所看到的,我正在指定一个时间间隔,在这个时间间隔中,结果事件必须是。 正如你所看到的,结束date是从现在开始的25年,而开始date是4年(加上闰年的一天)。 如果我以这种方式查询EKEventstore,我正在获取以前添加的事件。 如果我想把开始date放回过去一天(或更多天或几年),那么棘手的部分就开始了。 然后,突然,没有事件返回。

 NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:- 60 * 60 * 24 * (365 * 4 + 2)]; 

NSPredicate中NSTimeInterval的负值是否有限制? 我没有发现任何logging的限制。 我花了大约2个小时才发现为什么我没有收到任何活动(原来我想过去5年,未来5年)。 这个好奇的行为是什么原因? 任何想法?

/编辑于2012年4月11日2012年3月31日和2012年4月20日开始创build一些开始date之后,似乎是从现在开始按间隔确定的date提取事件受限于间隔长度为4年份。 调整我的开始date(通过设置提前一天的开始date)在上面给定的代码我能够得到事件,直到2012年3月31日,但不晚。 取消此调整导致从2012年3月31日和2012年4月1日(但不是从2012年4月20日)的事件。 经过第二次调整(20天后设置开始date),我甚至可以得到未来的事件。
我无法指出为什么会有这样的限制。 可能会有一些内部计算会导致使用价值存储溢出。 只是一个猜测。

然后我去苹果的例子。 乍一看,我不想在Apple的EKEvent编程指南中使用给定的代码。 它看起来并不像我的小巧可爱,但经过这么多的麻烦之后,我给了它一个镜头。

 CFGregorianDate gregorianStartDate, gregorianEndDate; CFTimeZoneRef timeZone = CFTimeZoneCopySystem(); gregorianStartDate.hour = 0; gregorianStartDate.minute = 0; gregorianStartDate.second = 0; gregorianStartDate.day = 1; gregorianStartDate.month = 4; gregorianStartDate.year = 2008; NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; gregorianEndDate.hour = 23; gregorianEndDate.minute = 59; gregorianEndDate.second = 59; gregorianEndDate.day = [components day]; gregorianEndDate.month = [components month]; gregorianEndDate.year = [components year] + 1; NSDate *startDate = [NSDate dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianStartDate, timeZone)]; NSDate *endDate = [NSDate dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianEndDate, timeZone)]; CFRelease(timeZone); 

通过这种方式,我可以获得从04/01/2008开始直到NOW()+ 1年的所有活动。 那么,事实certificate,在这里使用相同的限制:(调整开始date导致只获得我的事件的一部分,直到最后的事件在4年的范围内。

深入研究表明,这种行为存在很长时间: 从EventStore EventKit iOS获取所有事件

我同意詹姆斯将太多的事件牵扯到未来的日子里,但是幸运的是,你可以用遥远的未来来吸引远方的事件。

这显然不会拉动所有的事件,而是尽可能多地发生事件,从而导致内存或其他问题。

 NSDate *endDate = [NSDate distantFuture]; 

希望有所帮助

从CalendarStore查询如此大的date范围事件通常是一件坏事。 一次拉一个月的performance是可怕的,你想拉“全”? 绝对不build议。 让CalendarStore成为一个不必担心“全部”的问题,您应该关注如何正确确定需要提取的date范围。

例如,如果您有一个正常的日历应用每次显示一个月,只需拉下3个月(您的目标月份,再加上当前日历前后的月份)。 如果有人下一个月,然后提出请求,以获得一个月后,等等。

也许你可以提供更多的信息,是什么驱动你的愿望,“所有”的事件?