EKEventStore获取事件返回空列表

TRD*_*TRD 13 ios ekevent ekeventkit

我希望在我的应用中获取特定日历中的所有活动.我在我的应用程序中创建了日历和测试事件(需要iOS 5.0或更高版本才能创建自定义日历).如果我在我的设备上运行该应用程序,然后检查系统日历应用程序,那么我的日历和我创建的事件将正确显示.

现在我希望我的应用程序从这个自定义日历中读取所有这些事件.我的事件是在startDate和endDate设置为NOW(NSDate分配时没有给出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];
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在指定结果事件所必须的时间间隔.您也可以看到,结束日期是从现在开始的25年,而开始日期是4年(闰年加一天).如果我以这种方式查询EKEventstore,我将获得之前添加的事件.如果我想将开始日期放回过去一天(或多天或几年),那么棘手的部分就会开始.然后,突然间,没有返回任何事件.

NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:- 60 * 60 * 24 * (365 * 4 + 2)];
Run Code Online (Sandbox Code Playgroud)

NSPredicate中NSTimeInterval的负值是否有限制?我没有发现任何记录的限制.我花了大约2个小时才发现为什么我没有参加任何活动(最初我想要的是过去5年和未来5年).这种好奇行为的原因是什么?任何的想法?

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

然后我前往Apple的例子.乍一看,我不想在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);
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我可以获得从04/01/2008开始到NOW()+ 1年的所有活动.好吧,原来这里使用了相同的限制:(调整开始日期导致我的事件只有一部分,直到最后一次事件在这4年的范围内.

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

Jam*_*her 0

从 CalendarStore 查询如此大的日期范围的事件通常是一件坏事。一次拉一个月的性能很糟糕,而您想拉“全部”?绝对不建议。让 CalendarStore 成为必须担心“全部”的那个,您应该关注如何正确确定需要拉取的日期范围。

例如,如果您有一个一次显示一个月的普通日历应用程序,只需下拉 3 个月(您的目标月份,加上当前日历前后的月份)。如果有人寻呼到下个月,则发出请求以获取下个月的信息,依此类推。

也许您可以提供更多信息来说明是什么促使您渴望获得“所有”活动?