【问题标题】:Some EKEvents doesn't match predicate一些 EKEvents 与谓词不匹配
【发布时间】:2013-03-19 22:50:00
【问题描述】:

我正在开发一个日历应用程序。

在一个月的时间里,我整天都在循环以获取当天的事件。我必须这样做才能处理重复发生的事件和超过一天的事件。

除了一种情况外,它工作得很好:为期数天的活动的最后一天。我看到了这个事件的其他日子的事件,但没有看到最后一个事件。 (我在 GMT+1 时区,这就是我有这个时间的原因)

SEARCH FOR THE LAST DAY OF EVENT
Start:  2013-03-25 23:00:00 +0000
End:    2013-03-26 22:59:59 +0000

EVENT
Start:  2013-03-24 21:00:06 +0000
End:    2013-03-26 21:00:06 +0000

No results!

这是返回当天事件的方法:

+ (NSArray *)ekEventsWithStartDate:(NSDate*)startDate endDate:(NSDate*)endDate
{
    NSLog(@"ekEventsWithStartDate:%@ endDate:%@",startDate,endDate);
    NSPredicate *predicate = [_eventStore predicateForEventsWithStartDate:startDate
                                                                  endDate:endDate
                                                                calendars:nil];

    NSArray *events = [_eventStore eventsMatchingPredicate:predicate];
    NSLog(@"events (%d):%@",[events count],events);
    return events;
}

以下是活动详情:

EKEvent <0xb0635e0> {EKEvent <0xb0635e0> 
{title =  24-26 Mars 10 PM; 
 location = ; 
 calendar = EKCalendar <0xb3c3c80> {title = Calendar; type = Local; allowsModify = YES; color = #0E61B9;}; 
 alarms = (null);  
 URL = (null); 
 lastModified = 2013-03-19 22:11:10 +0000; 
 timeZone = Europe/Paris (GMT+01:00) offset 3600}; 
 location = ; 
 startDate = 2013-03-24 21:00:06 +0000; 
 endDate = 2013-03-26 21:00:06 +0000; 
 allDay = 0; 
 floating = 0; 
 recurrence = (null); 
 attendees = (null)}

这是来自 ekEventsWithStartDate 方法的该事件 3 天的日志:

ekEventsWithStartDate:2013-03-23 23:00:00 +0000 endDate:2013-03-24 22:59:59 +0000
events (1):(
    "EKEvent <0x9b44c00> {EKEvent <0x9b44c00> {title =  24-26 Mars 10 PM; location = ; calendar = EKCalendar <0xb336870> {title = Calendar; type = Local; allowsModify = YES; color = #0E61B9;}; alarms = (null); URL = (null); lastModified = 2013-03-19 22:11:10 +0000; timeZone = Europe/Paris (GMT+01:00) offset 3600}; location = ; startDate = 2013-03-24 21:00:06 +0000; endDate = 2013-03-26 21:00:06 +0000; allDay = 0; floating = 0; recurrence = (null); attendees = (null)}"
)

ekEventsWithStartDate:2013-03-24 23:00:00 +0000 endDate:2013-03-25 22:59:59 +0000
events (1):(
    "EKEvent <0xb28b970> {EKEvent <0xb28b970> {title =  24-26 Mars 10 PM; location = ; calendar = EKCalendar <0xb336870> {title = Calendar; type = Local; allowsModify = YES; color = #0E61B9;}; alarms = (null); URL = (null); lastModified = 2013-03-19 22:11:10 +0000; timeZone = Europe/Paris (GMT+01:00) offset 3600}; location = ; startDate = 2013-03-24 21:00:06 +0000; endDate = 2013-03-26 21:00:06 +0000; allDay = 0; floating = 0; recurrence = (null); attendees = (null)}"
)

ekEventsWithStartDate:2013-03-25 23:00:00 +0000 endDate:2013-03-26 22:59:59 +0000
events (0):(null)

为什么方法返回空数组?

辅助问题:有没有更好的方法来获取一个月中每一天的事件?我正在寻找更好的表现。

感谢您的帮助!

2013 年 3 月 20 日编辑: 多亏了 Dhruvik,我发现我的代码在 iOS 5.X 上完美运行,但在 iOS 6.X 上却不行(4.X 没有测试)。

我检查了 5.X 和 6.X 版本的事件和日期,我看到的唯一区别是事件日历时区属性:

iOS 5.X
timeZone = Europe/Paris (CET)

iOS 6.X
timeZone = Europe/Paris (UTC+01:00)

此问题与全天活动无关。

iOS 6.X 有同样的问题吗?

【问题讨论】:

    标签: iphone ios objective-c ekeventkit


    【解决方案1】:

    这是我在我的应用程序中实现的 fecth 事件的代码。

    在.h中

    @property (nonatomic, retain) EKEventStore *eventStore;
    @property (nonatomic, retain) EKCalendar *defaultCalendar;
    @property (nonatomic, retain) NSMutableArray *eventsList;
    

    在.m

    //This code will fecth the events from one day Before the current date..
    
    - (void) fetchevents
    {
    
       eventStore = [[EKEventStore alloc] init];
    
       eventsList = [[NSMutableArray alloc] init];
    
       EKEventStore *store = [[EKEventStore alloc] init];
    
       [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
           // handle access here
       }];
    
       // Get the appropriate calendar
       NSCalendar *calendar = [NSCalendar currentCalendar];
    
       // Create the start date components
       NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
       oneDayAgoComponents.day = -1; // From which date you have to fetch Events from calander, as per your need subtract the days from current date
    
       NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                  toDate:[NSDate date]
                                                 options:0];
    
       NSLog(@"%@", oneDayAgo);
    
       // Create the end date components
       NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
       oneYearFromNowComponents.year = 0;
       NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
                                                       toDate:[NSDate date]
                                                      options:0];
       NSLog(@"%@", oneYearFromNow);
    
       //Create the predicate from the event store's instance method
       NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow                                                                                      calendars:nil];
    
    
       NSArray *events_Array = [eventStore eventsMatchingPredicate: predicate];
    
       for (EKEvent *eventToCheck in events_Array)
       {
           [eventsList addObject:eventToCheck.title ];
       }
    }
    

    希望对您有所帮助。祝您编码愉快。。谢谢

    【讨论】:

    • 感谢您的代码。我创建了一个新的空项目来测试它(它非常接近我的代码)并且我遇到了同样的问题。但是由于您的回答,我在几个 iOS 中进行了测试,我发现我的问题并未出现在 iOS 5.X 上,而只是出现在 6.X 上(我没有测试 4.X)。我想知道这是否是苹果的回归。
    • okk.. 是的,它可能是苹果公司的回归。那么你有没有从我的代码中得到完整的解决方案?
    • 不,我的代码和你一样,但它不适用于 iOS 6.X
    • 好的,您必须使用 3 天的事件(不是全天模式)进行测试,并尝试在第 3 天(00:00)开始时获取开始日期的事件和第 3 天结束时的结束日期 (23:59)
    • @Dhruvik 如果我们能在任何特定事件发生时触发,你能帮我吗?
    猜你喜欢
    • 1970-01-01
    • 2020-04-29
    • 2014-07-26
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多