EKEvent eventIdentifier returns null

这一生的挚爱 提交于 2020-01-01 10:04:25

问题


When I try to get the identifier of an EKEvent, all I get is a nil value. Since in iOS5 EKEvent is a subclass of EKCalendarItem, I figured I might be able to get the EKCalendarItem's UUID, but that returns nil as well.

Every now and then I also get this error while trying to access the identifier or UUID property:

CADObjectGetInlineStringProperty failed fetching uniqueID for EKPersistentEvent with error Error Domain=NSMachErrorDomain Code=268435459 "The operation couldn’t be completed. (Mach error 268435459 - (ipc/send) invalid destination port)"

I've been stuck on this problem for quite some time now, but figured it would be iOS5 beta related. But since we're now at iOS5, it's still not working.


回答1:


When I try to get the identifier of an EKEvent, all I get is a nil value

Try to save AND commit your event before retrieving the identifier :

[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *strId = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];



回答2:


In my app I found out that if you ask for the eventIdentifier when the eventStore that fetched it has been released, it returns nil. But if you ask for the eventIdentifier before it will return the id ok. You can then release the EKEventStore instance and ask for the identifier with no problem.... Seems that it needs the eventStore to retrieve the id, but I get no warnings.




回答3:


eventIdentifier is set when the event is added to the EKEventStore. If you try to access this value before adding it, it would return null.




回答4:


Just going through this problem, turnout the eventIdentifier will be null before commit to database, so you need a commit:YES in the saveEvent function [self.eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];

After that, you can get the eventIdentifier.

My fault was pass a NO to commit: parameter.




回答5:


For me, the eventIdentifier is null because I was not setting the endDate. So generally the eventIdentifier can be null if there are any error on creating that event. You can check for errors like this:

NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSLog(@"Error : %@",err);



回答6:


The EKEvent eventIdendifier isn't generated until the event is saved. You can access/store the eventIdentifier after you've saved the event to the EKEventStore

    [store saveEvent:event span:EKSpanThisEvent error:&err];
    NSString *eventIdentifier = event.eventIdentifier;



回答7:


For Swift 3

I have found out, that the issue was that I have created the store within the function that retrieves the date.

Creating the store outside of the function and using its instance solved the issue.

class CalendarServices: NSObject {

    var store = EKEventStore()

    func fetchEventKitCalendarEvents(date: Date, completion: @escaping (_ events: [EKEvent]?, _ error: Error?)->()) {

        let calendar = Calendar.current

        guard self.getEventKitAuthorizationStatus() == .authorized else {
            completion(nil, CoreServices.setError(message: "AuthorizationStatus != authorized"))
            return
        }

        guard let endDate = calendar.date(byAdding: .day, value: 1, to: date)  else {
            completion(nil, CoreServices.setError(message: "Error creating endDate"))
            return
        }

        CoreServices.background {

            let predicate = self.store.predicateForEvents(withStart: date, end: endDate, calendars: self.fetchEventKitCalendars())

            let events = self.store.events(matching: predicate).sorted() {
                (e1: EKEvent, e2: EKEvent) -> Bool in
                return e1.startDate.compare(e2.startDate) == .orderedAscending
            }

            CoreServices.async {

                completion(events, nil)

            }

        }

    }

}


来源:https://stackoverflow.com/questions/7768751/ekevent-eventidentifier-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!