iOS7 Calendar - Predicate call to calendar daemon failed: Error Domain=EKCADErrorDomain Code=1013

1.1k views Asked by At

I am receiving the following error on the first run of my app in an iOS7 app that I am trying to integrate with an existing project and can not figure out how to eliminate this error.

"Predicate call to calendar daemon failed: Error Domain=EKCADErrorDomain Code=1013"

The code that triggers the error on the first run of the app is:

  NSMutableArray *events =  [NSMutableArray arrayWithArray:[self.eventStore eventsMatchingPredicate:predicate]];

Do I need to check if the eventStore (in the last function) is empty before using the predicate statement?

The functions definitions are below:

This function makes sure the user has granted permission to access calendar.

// Prompt the user for access to their Calendar
    -(void)requestCalendarAccess
    {
        if([self.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
        {
            [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
             {
                 if (granted)
                 {
                     // The user has granted access to their Calendar; let's populate our UI with all events occuring in the next 24 hours.
                     [self accessGrantedForCalendar];
                 }
             }];
        }
    }

Function below is called when permission is granted.

    // This method is called when the user has granted permission to Calendar
    -(void)accessGrantedForCalendar
    {
        // Let's get the default calendar associated with our event store
        self.defaultCalendar = PierceCalendar;
        // Enable the Add button
        self.addButton.enabled = YES;
        // Fetch all events happening in the next 24 hours and put them into eventsList
        self.eventsList = [self parseEventsByMonth];
    }

The first line of the function calls fetchEvents function that contains the error causing statement.

-(NSMutableArray *) parseEventsByMonth
{
    NSMutableArray *allPierceEvents = [self fetchEvents];
     ....
    return parsedEventsByMonth;
}

This function executes the statement ([self.eventStore eventsMatchingPredicate:predicate]) that is causing the error on the first run of the app.

// Fetch all events happening in the next 24 hours
- (NSMutableArray *)fetchEvents
{
    NSMutableArray *pierceEvents = [[NSMutableArray alloc] init];

    //Create the end date components
    NSDateComponents *oneYearAgoComponents = [[NSDateComponents alloc] init];
    oneYearAgoComponents.year = -1;

    NSDate *startDate = [[NSCalendar currentCalendar] dateByAddingComponents:oneYearAgoComponents
                                                                      toDate:[NSDate date]
                                                                     options:0];

    NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
    oneYearFromNowComponents.year = 1;
    NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:oneYearFromNowComponents
                                                                    toDate:[NSDate date]
                                                                   options:0];

    // We will only search the default calendar for our events
    NSArray *calendarArray = [self.eventStore calendarsForEntityType:EKEntityTypeEvent];
    // Create the predicate
    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
                                                                      endDate:endDate
                                                                    calendars:calendarArray];

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

    for (EKEvent *event in events)
    {
        if (event.calendar == PierceCalendar) {
            [pierceEvents addObject:event];
        }
    }

    return pierceEvents;
}
1

There are 1 answers

0
Vahe On BEST ANSWER

I inserted the statement before the line causing the error to eliminate the error.

[self.eventStore reset];

I found the hint from:

calendarsForEntityType:EKEntityTypeReminder is empty first time, works subsequently