简体   繁体   English

EventKit日历的最后一个事件

[英]EventKit last event of calendar

I'm making an app in which I sync certain events to a calendar on iPhone. 我正在开发一个将某些事件同步到iPhone上的日历的应用。

The problem is, I have no way of telling which events were altered/removed/... So I need to remove all the events between today and the end date of the last event of the calendar before 'syncing' (read inserting) the new events. 问题是,我无法告诉您哪些事件已被更改/删除/ ...所以我需要在“同步”(读取插入)之前删除今天和日历最后一个事件的结束日期之间的所有事件。新事件。

As far as I've seen, the only way to do an action on multiple events at once, is by using enumerateEventsMatchingPredicate:usingBlock: and predicateForEventsWithStartDate:endDate:calendars: 据我所知,一次对多个事件执行操作的唯一方法是使用enumerateEventsMatchingPredicate:usingBlock:predicateForEventsWithStartDate:endDate:calendars:

But for this I need a specific end date. 但是为此,我需要一个特定的结束日期。 (and thus, the end date of the last event in my calendar) (因此,是我日历中最后一个活动的结束日期)

I could always save the event identifier of the last event I insert into this calendar, but I would rather not do this: If the user uninstalls my app and installs it again later, I don't have the last event identifier anymore. 我总是可以将我插入的最后一个事件的事件标识符保存到该日历中,但我不愿意这样做:如果用户卸载了我的应用程序并在以后再次安装,则我不再有最后一个事件标识符。 (given that (s)he didn't remove the calendar manually of course) (鉴于他当然没有手动删除日历)

I could just remove the calendar every time I need to sync the calendar, but then I would lose all passed events. 每当我需要同步日历时,我都可以删除日历,但是那样我会丢失所有通过的事件。

Any ideas or tips are much appreciated! 任何想法或技巧,不胜感激!

For your comment on : 对于您的评论:

I can't seem to find any way to fetch all the events of a calendar. 我似乎找不到任何方法来获取日历的所有事件。

Actually you can fetch all the events from calendar : 实际上,您可以从calendar获取所有事件:

NSDate *start = ...
NSDate *finish = ...

// use Dictionary for remove duplicates produced by events covered more one year segment
NSMutableDictionary *eventsDict = [NSMutableDictionary dictionaryWithCapacity:1024];

NSDate* currentStart = [NSDate dateWithTimeInterval:0 sinceDate:start];

int seconds_in_year = 60*60*24*365;

// enumerate events by one year segment because iOS do not support predicate longer than 4 year !
while ([currentStart compare:finish] == NSOrderedAscending) {

    NSDate* currentFinish = [NSDate dateWithTimeInterval:seconds_in_year sinceDate:currentStart];

    if ([currentFinish compare:finish] == NSOrderedDescending) {
        currentFinish = [NSDate dateWithTimeInterval:0 sinceDate:finish];
    }
    NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:currentStart endDate:currentFinish calendars:nil];
    [eventStore enumerateEventsMatchingPredicate:predicate
                                      usingBlock:^(EKEvent *event, BOOL *stop) {

                                          if (event) {
                                              [eventsDict setObject:event forKey:event.eventIdentifier];
                                          }

                                      }];       
    currentStart = [NSDate dateWithTimeInterval:(seconds_in_year + 1) sinceDate:currentStart];

}

NSArray *events = [eventsDict allValues];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM