简体   繁体   中英

Can't add/change alarms EKAlarm to a [newly created] EKEvent event

I'm trying to create an event with one alarm programmatically like that:

    +(void)exportEvent:(AgendaEvent*)evento
                    onCalendar:(EKCalendar*)calendar {

        EKEventStore* store= [[[EKEventStore alloc] init] autorelease];
        [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if(!granted) {
                    // show "not granted" message
                    return;
                }
                // save event
                NSCalendar* gc= [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
                EKEvent* event= [EKEvent eventWithEventStore:store];
                event.title= evento.descrizione;
                event.startDate= [gc dateFromComponents:evento.begin];
                if(evento.end)
                    event.endDate= [gc dateFromComponents:evento.end];
                else {
                    NSDateComponents* endDateComponents= [[evento.begin copy] autorelease];
                    endDateComponents.day++;
                    endDateComponents.hour= 0;
                    endDateComponents.minute= -1;
                    endDateComponents.second= 0;
                    NSDate* endDate= [gc dateFromComponents:endDateComponents];
                    // endDate is correctly set at 23:59 of the same day of beginDate, when all day beginDay is at 00:00
                    event.endDate= endDate;
                    event.allDay= YES;
                }
                event.calendar= calendar;
                // reminder
                NSDateComponents* reminderDateComponents= [[evento.begin copy] autorelease];
                reminderDateComponents.day--;
                reminderDateComponents.hour= 9;
                reminderDateComponents.minute= 0;
                NSDate* reminderDate= [gc dateFromComponents:reminderDateComponents];
                // reminder date is correctly set at 9:00 of the previous day of beginDate
                [event addAlarm:[EKAlarm alarmWithAbsoluteDate:reminderDate]];

                NSError* err= nil;
                [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
                if(err) {
                    // show "unable to export" message
                    return;
                }
                // show "exported" message
            });
        }];
    }

but some times only (or more correctly often) [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err] fails with:

    2014-06-13 09:34:01.300 xxx[224:60b] CADObjectGetRelation failed with error Error Domain=NSMachErrorDomain Code=268435459 "The operation couldn’t be completed. (Mach error 268435459 - (ipc/send) invalid destination port)"
    2014-06-13 09:34:01.301 xxx[224:60b] Impossibile esportare evento: Error Domain=EKErrorDomain Code=29 "Impossibile modificare avvisi." UserInfo=0x16a7c7e0 {NSLocalizedDescription=Impossibile modificare avvisi.}

I can't even find a description for code 29 in EKErrorDomain, does anybody have a clue?

Please mind that:

  • I'm not using arc as you can see but seems pretty correct to me (an to static analyzer too).
  • I've also tried to split the event save in two phases: one for the event and one for the alarm with exactly the same results.
  • "Impossibile modificare avvisi." means "Can not change alerts."
  • Tried on an iPad air with ios7.1.1 and ios7.1 simulator
  • CADObjectGetRelation related message is not always shown even if event creation fails but seems to not appear when the event and alarms are created.

Ok after some trial and error I managed to get it working. The problem is that just before calling exportEvent: I was creating another EKEventStore to read and select an EKCalendar calendar.

I removed store from the question selector body and passed it as a parameter from the previous step and now it works. This, I suppose, because there is some ipc involved and store is deallocated between user calendar selection step and the actual event creation. If not enough time passes between deallocation in step1 and reallocation in step2 ipc connection gets denied which explains why sometimes it was working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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