3

I know that code below is very useful for create and add an event to iOS calendar. It work great and i can add many events, but with one event i have to make one touch on the button.

- (IBAction)add_event:(id)sender{     
    EKEventStore *eventStore=[[EKEventStore alloc] init];
    EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore];
    addEvent.title=@"title";
    addEvent.startDate = [NSDate date];
    addEvent.endDate = [addEvent.startDate dateByAddingTimeInterval:600];
    [addEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
    NSError *err;
    [eventStore saveEvent:addEvent span:EKSpanThisEvent error:&err];
    if (err == nil) {
        NSString* str = [[NSString alloc] initWithFormat:@"%@", addEvent.eventIdentifier];
        NSLog(@"Event ID: %@" , str );
    }
    else {
        NSLog(@"Error %@",err);
    } 
}

Then i try to add many events with a while loop on the button touch event, my edited code here:

- (IBAction)add_event:(id)sender{
    int i = 0;
    while(i < 100){
        EKEventStore *eventStore=[[EKEventStore alloc] init];
        EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore];
        addEvent.title=@"title";
        addEvent.startDate = [NSDate date];
        addEvent.endDate = [addEvent.startDate dateByAddingTimeInterval:600]; 
        [addEvent setCalendar:[eventStore defaultCalendarForNewEvents]]; 
        NSError *err;
        [eventStore saveEvent:addEvent span:EKSpanThisEvent error:&err];
        if (err == nil) {
            NSString* str = [[NSString alloc] initWithFormat:@"%@", addEvent.eventIdentifier];
            NSLog(@"Event ID %d: %@",i, str);
        }
        else {
            NSLog(@"Error %@",err);
        }
        i++;
    }
}

I want to add 100 events to calendar, but only 82 events add successful, all events from number 83 make some errors. Here is my log screen:

.......

2013-03-07 15:08:07.742 MyDTUSchedule[3066:c07] Event ID 79: BBCF7782-5D60-42D7-8478-EF80604FBF41:B0124DEE-EC5F-40B9-B9F8-312FA07D8059
2013-03-07 15:08:07.756 MyDTUSchedule[3066:c07] Event ID 80: BBCF7782-5D60-42D7-8478-EF80604FBF41:613F794D-67BE-4704-BEC2-7439E77965F0
2013-03-07 15:08:07.781 MyDTUSchedule[3066:c07] Event ID 81: BBCF7782-5D60-42D7-8478-EF80604FBF41:2FEF3B6D-6AC0-4058-AA79-BB46FEBDF732
2013-03-07 15:08:07.810 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6a380 {NSLocalizedDescription=No calendar has been set.}
2013-03-07 15:08:07.812 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6a9d0 {NSLocalizedDescription=No calendar has been set.}
2013-03-07 15:08:07.813 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6c310 {NSLocalizedDescription=No calendar has been set.}
2013-03-07 15:08:07.815 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6d5d0 {NSLocalizedDescription=No calendar has been set.}

...........

Would you please help me resolve this problem? I run this code in iOS Simulator 5.0. The iOS Simulator 6.0 make the same error but at event number 124

1 Answer 1

2

It might be a memory issue. eventStore is allocated on each loop iteration and never released.

Try put EKEventStore *eventStore=[[EKEventStore alloc] init]; out from the loop.

2
  • Thanks for your help. A few days a ago i tried to declare eventStore object outside my button touch event but not work. Now I do that again and it seem OK.
    – nghien_rbc
    Commented Mar 8, 2013 at 7:06
  • Everytime i launch the app for the first time, it does not work.. it works from subsequent launch of the app.. tried everything.. created strong EKEventStore property and compared with if(!self.eventSore){ self.eventStore = [[EKEventStore alloc] init];} in viewDidLoad.. what could be the solution? Commented Nov 17, 2015 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.