EKEventStore获取事件返回空列表 [英] EKEventStore getting events returns empty list

查看:150
本文介绍了EKEventStore获取事件返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get all events from a specific calendar in my app. I created the calendar and the test events in my app (needs iOS 5.0 or later for creating custom calendars). If I run the app on my device and then check the system calendar app then my calendar and my created events are shown correctly.

现在我想让我的应用程序从这个自定义日历中读取所有这些事件。我的事件是在startDate和endDate设置为NOW(NSDate分配时没有给出timeInterval)的情况下创建的。

Now I want my app to read all those events out of this custom calendar. My events are created with startDate and endDate set to NOW (NSDate alloced with no timeInterval given).

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];

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

As you can see I am specifying a time interval in which the resulting events have to be. As you can see too, the end date is in 25 years from now on whereas the start date is 4 years (plus one day for leap year) in the past. If I query the EKEventstore in this way, I am getting the previously added events. The tricky part begins if I want to put the start date one (or more days or years) back in the past. Then, suddenly, no events are returned.

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

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

Is there a limit for negative values of a NSTimeInterval in a NSPredicate? I did not found any documented limitations. It took me about 2 hours to detect why I was getting no events (originally I wanted a range of 5 years in the past and 5 years in the future). What is the reason for this curious behaviour? Any idea?

/编辑于04/11/2012
创建一些事件的开始日期为03/31/2012和04/20/2012似乎是按照从现在开始的间隔确定的日期获取事件受限于4年的间隔长度。在上面的给定代码中调整我的开始日期(通过设置前一天的开始日期),我能够在2012年3月31日之前获得事件,但不能更晚。删除此调整导致从03/31/2012和04/01/2012获取事件(但不是2012年4月20日的事件)。经过第二次调整(设置开始日期20天后),我甚至得到了那些未来的事件。

我无法说明为何存在这样的限制。可能有一些内部计算会导致使用值存储溢出。只是一个猜测。

/edit on 04/11/2012 After creating some events with a starting date on 03/31/2012 and 04/20/2012 it seems to be that fetching events by dates determined with intervals from now is limited by an interval length of 4 years. Adjusting my start date (by setting the start day one day earlier) in the given code above I was able to get events until 03/31/2012 but not later ones. Removing this adjustment resulted in getting events from 03/31/2012 and 04/01/2012 (but not those from 04/20/2012). After a second adjustment (setting the start date 20 days later) I got even those future events.
I cannot specify why there is such limitation. May be there are some internal calculations that would lead to overflows of the used value storage. Just a guess.

然后我前往 Apple 示例。乍一看,我不想在Apple的EKEvent编程指南中使用给定的代码。它看起来并不小。我很可爱,但是经过这么多麻烦之后我就试了一下。

Then I head to Apple examples. At first glance I did not want to use the given code in the EKEvent Programming Guide of Apple. It did not look as small & cute as mine, but after having so much trouble I gave it a shot.

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开始到现在()+ 1年的所有活动。好吧,事实证明这里使用了相同的限制:(调整开始日期导致我的事件只有一部分,直到最后一次事件都在这4年的范围内。

This way I get all events starting from 04/01/2008 up to NOW() + 1 year. Well, it turned out that the same limitation is used here :( Adjusting the start date resulted in getting only a part of my events until the last events were in the range of those 4 years.

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

Intensive researches showed up that this behaviour exists a very long time: Fetch all events from EventStore EventKit iOS

推荐答案

我同意James在日期中提取太多事件不建议到目前为止,幸运的是你可以使用 distantFuture 来吸引远方的事件。

I agree with James pulling too many events in with a date that is so far in the future is not advised, luckily you can use distantFuture to pull in, well distant events.

这显然不会全部拉动事件,但尽可能多的事件导致内存或其他问题。

This obviously does not pull all events but as many events as it can in with out causing memory or other issues.

NSDate *endDate = [NSDate distantFuture];

希望有所帮助

这篇关于EKEventStore获取事件返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆