使用事件工具包在指定的时间间隔内获取事件不能按预期工作

时间:2011-04-01 13:31:52

标签: iphone ios ipad

我在iOS 4.3.1上使用Event Kit来检索指定时间间隔内的所有事件。 但是,我遇到了以下问题。

1)使用开始日期[NSDate distantPast]和结束日期[NSDate distantFuture]不会返回任何事件。

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

    // Create the predicate.
    NSPredicate *eventPredicate = [store predicateForEventsWithStartDate:[NSDate distantPast] endDate:[NSDate distantFuture] calendars:store.calendars]; 

    // Fetch all events that match the predicate.
    NSArray *events = [[store eventsMatchingPredicate:eventPredicate] retain];

2)将开始日期设置为10年前,将结束日期设置为10年后,仅提取截至2005年的事件。不返回任何其他内容。

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

    CFGregorianDate gregorianStartDate, gregorianEndDate;
    CFGregorianUnits startUnits = {-10, 0, 0, 0, 0, 0};
    CFGregorianUnits endUnits = {10, 0, 0, 0, 0, 0};
    CFTimeZoneRef timeZone = CFTimeZoneCopySystem();

    gregorianStartDate = CFAbsoluteTimeGetGregorianDate(
                                                        CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), 
                                                        timeZone, 
                                                        startUnits),
                                                        timeZone);
    gregorianStartDate.hour = 0;
    gregorianStartDate.minute = 0;
    gregorianStartDate.second = 0;

    gregorianEndDate = CFAbsoluteTimeGetGregorianDate(
                                                      CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), 
                                                      timeZone,
                                                      endUnits),
                                                      timeZone);
    gregorianEndDate.hour = 0;
    gregorianEndDate.minute = 0;
    gregorianEndDate.second = 0;

    NSDate* startDate =
    [NSDate dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianStartDate, timeZone)];
    NSDate* endDate =
    [NSDate dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianEndDate, timeZone)];

    CFRelease(timeZone);

    // Create the predicate.
    NSPredicate *eventPredicate = [store predicateForEventsWithStartDate:startDate endDate:endDate calendars:store.calendars]; 

    // Fetch all events that match the predicate.
    NSArray *events = [[store eventsMatchingPredicate:eventPredicate] retain];

所以,我想知道发布的代码片段是否正确或者我是否做错了什么。理想情况下,我想使用[NSDate distantPast]和[NSDate distantFuture]在一次调用中检索所有日历中的所有事件。这有可能吗?此外,由于即使第二个例子也不起作用(至少对我而言),检索所有事件的正确方法是什么?我需要一个时间间隔很短的多个电话吗?如果是,可用于检索事件的最大时间间隔是多少? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

AFAIK,这是正确的。这部分代码看起来不像是错误。 我将你的第二个片段打入了一个临时应用程序。

    CFGregorianDate gregorianStartDate, gregorianEndDate;
    CFGregorianUnits startUnits = {-10, 0, 0, 0, 0, 0};
    CFGregorianUnits endUnits = {10, 0, 0, 0, 0, 0};
    CFTimeZoneRef timeZone = CFTimeZoneCopySystem();

    gregorianStartDate = CFAbsoluteTimeGetGregorianDate(
                                                        CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), 
                                                        timeZone, 
                                                        startUnits),
                                                        timeZone);
    gregorianStartDate.hour = 0;
    gregorianStartDate.minute = 0;
    gregorianStartDate.second = 0;

    gregorianEndDate = CFAbsoluteTimeGetGregorianDate(
                                                      CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), 
                                                      timeZone,
                                                      endUnits),
                                                      timeZone);
    gregorianEndDate.hour = 0;
    gregorianEndDate.minute = 0;
    gregorianEndDate.second = 0;

    NSDate* startDate =
    [NSDate dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianStartDate, timeZone)];
    NSDate* endDate =
    [NSDate dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianEndDate, timeZone)];

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease];
[dateFormatter setLocale:usLocale];

NSDate * today = [[[NSDate alloc] init] autorelease];

    NSLog(@"Date for locale %@: %@",[[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:today]);
    NSLog(@"Date for locale %@: %@",[[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:startDate]);
    NSLog(@"Date for locale %@: %@",[[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:endDate]); 

    CFRelease(timeZone);

我得到了以下输出:

2011-11-23 13:31:40.244 Testapp [15440:207] locale en_US日期:2011年11月23日

2011-11-23 13:31:41.195 Testapp [15440:207] locale en_US的日期:2001年11月23日

2011-11-23 13:31:42.063 Testapp [15440:207] locale en_US日期:2021年11月23日

祝你好运。

顺便说一下,我会使用NSDateComponent来获得未来10年和过去10年。 将其添加到上面的代码中,

NSDateComponents *yearForPredicate = [[[NSDateComponents alloc] init]autorelease];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendar:NSGgregorianCalendar]autorelease];
[yearForPredicate setYear:10];
NSDate *distantFuture = [gregorian dateByAddingComponents:yearForPredicate toDate:today options:0];
[yearForPredicate setYear:-10]
NSDate *distantPast = [gregorian dateByAddingComponents:yearForPredicate toDate:today options:0];