簡體   English   中英

EKEvent eventIdentifier返回null

[英]EKEvent eventIdentifier returns null

當我嘗試獲取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 beta相關。 但由於我們現在處於iOS5,它仍然無法運行。

當我嘗試獲取EKEvent的標識符時,我得到的只是一個零值

在檢索標識符之前嘗試保存並提交事件:

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

在我的應用程序中,我發現如果你在獲取它的eventStore被釋放時請求eventIdentifier,它將返回nil。 但是如果你要求eventIdentifier之前它會返回id ok。 然后,您可以釋放EKEventStore實例並請求標識符沒有問題....似乎它需要eventStore來檢索id,但我沒有得到任何警告。

剛剛解決這個問題,在提交數據庫之前, eventIdentifier將為null,因此你需要在saveEvent函數中使用commit: YES [self.eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];

之后,您可以獲取eventIdentifier。

我的錯是傳遞NO來提交:參數。

將事件添加到EKEventStore時設置eventIdentifier。 如果在添加它之前嘗試訪問此值,它將返回null。

對我來說, eventIdentifier為null,因為我沒有設置endDate 因此,如果在創建該事件時出現任何錯誤,則eventIdentifier通常可以為null。 你可以檢查這樣的錯誤:

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

在保存事件之前,不會生成EKEvent eventIdendifier。 將事件保存到EKEventStore后,可以訪問/存儲eventIdentifier

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

對於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)

            }

        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM