EKEvent eventIdentifier返回null

时间:2011-10-14 14:00:04

标签: iphone objective-c ios5 eventkit ekevent

当我尝试获取EKEvent的标识符时,我得到的只是一个零值。因为在iOS5中EKEvent是EKCalendarItem的子类,所以我想我可能能够获得EKCalendarItem的UUID,但也会返回nil。

在尝试访问标识符或UUID属性时,我偶尔会遇到此错误:

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

我已经在这个问题上坚持了很长一段时间,但我认为它与iOS5测试版有关。但由于我们现在处于iOS5,它仍然没有用。

7 个答案:

答案 0 :(得分:7)

  

当我尝试获取EKEvent的标识符时,我得到的只是一个零值

尝试在检索标识符之前保存并提交事件:

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

答案 1 :(得分:7)

在我的应用程序中,我发现如果你在获取它的eventStore被释放时请求eventIdentifier,它将返回nil。但是如果你要求eventIdentifier之前它会返回id ok。然后,您可以释放EKEventStore实例并请求标识符没有问题....似乎需要eventStore来检索id,但我没有收到任何警告。

答案 2 :(得分:2)

将事件添加到EKEventStore时设置

eventIdentifier。如果在添加之前尝试访问此值,则会返回null。

答案 3 :(得分:2)

刚刚解决这个问题,在提交数据库之前,eventIdentifier的结果为null, 所以你需要在saveEvent函数YES

中提交[self.eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];

之后,您可以获取eventIdentifier。

我的错是传递NO来提交:参数。

答案 4 :(得分:0)

对我来说,eventIdentifier为空,因为我没有设置endDate。因此,如果在创建该事件时出现任何错误,则eventIdentifier通常可以为null。你可以检查这样的错误:

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

答案 5 :(得分:0)

在保存事件之前,不会生成EKEvent eventIdendifier。将事件保存到EKEventStore

后,可以访问/存储eventIdentifier
    [store saveEvent:event span:EKSpanThisEvent error:&err];
    NSString *eventIdentifier = event.eventIdentifier;

答案 6 :(得分:0)

Swift 3

我发现问题是我在检索日期的函数中创建了商店。

在函数外部创建商店并使用其实例解决了这个问题。

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)

            }

        }

    }

}