EKEvent eventIdentifier returns null

和自甴很熟 提交于 2019-12-04 06:06:20

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];

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.

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.

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.

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);

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;

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)

            }

        }

    }

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