12

i would like to know how to fetch all events from an EventStore using EventKit in iOS.

This way i can specify all events for today:

- (NSArray *)fetchEventsForToday {

    NSDate *startDate = [NSDate date];

    // endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
    NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400];

    // Create the predicate. Pass it the default calendar.
    NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray]; 

    // Fetch all events that match the predicate.
    NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];

    return events;
}

The correct should use a NSPredicate, which is created with:

NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray]; 

I have tried using

distantPast
distantFuture

as startDate and endDate, no good. So other A's from other Q's are not exaclty wha im looking for.

Thank you!


EDIT

I have tested and got to the conclusion that i can only fetch events in a period of 4 years maximum. Any way of getting past this? Without using multiple fetches..

Nicolas S
  • 5,325
  • 3
  • 29
  • 36

3 Answers3

13

Code for fetch all events into array :

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];
  • Thank you Dmitry! i will try it and come back! No one could answer this simple question, lets hope you did! – Nicolas S Jan 17 '12 at 05:34
  • This code will work, except that it consolidates all recurring events by only showing the final event returned, since it uses eventIdentifier as the hash key, and all recurring events have the same hash key. if (event) { [eventsDict setObject:event forKey:[event.eventIdentifier stringByAppendingString:[event.startDate description]]]; } – Tom Meyer May 03 '13 at 21:10
  • Watch out: not each year has 365 days.. So hardcoding that is not the best solution. – Jeroen Noten Nov 17 '14 at 20:17
  • You may want to call predicateForEventsWithStartDate in a different thread. – Shirish Kumar Mar 09 '15 at 16:58
0

This is the method I am using in my app to fetch them.

    NSDate *startDate = [NSDate distantPast];       
    NSDate *endDate = [NSDate distantFuture];
user794543
  • 84
  • 1
  • 6
-1

This is code in production

const double secondsInAYear = (60.0*60.0*24.0)*365.0;
NSPredicate* predicate = [eventStore predicateForEventsWithStartDate:[NSDate dateWithTimeIntervalSinceNow:-secondsInAYear] endDate:[NSDate dateWithTimeIntervalSinceNow:secondsInAYear] calendars:nil];

For you, I would recommend looking back and forward ten years.

MrAnonymous
  • 717
  • 3
  • 7