Search code examples
iosswiftcalendarekeventekeventstore

App crash while retrieve event with Identifier from calendar


When user delete the event manually from the calendar and app try to retrieve that EKEvent with identifier from EKEventStore then app is getting crash with the below error.

Error getting event with identifier AB78AC19-BD17-4290-8A21-4BF948089C12:66F6F5BC-BF61-401A-B808-44A555F7E63E: Error Domain=EKCADErrorDomain Code=1010 "The operation couldn’t be completed. (EKCADErrorDomain error 1010.)" fatal error: unexpectedly found nil while unwrapping an Optional value

Here is the code use to fetch event with identifier

let eventStore = EKEventStore()
let event = eventStore.event(withIdentifier: identifier as String)!

Please help me out from this.


Solution

  • Check if event exists:

    let eventStore = EKEventStore()
    if let event = eventStore.event(withIdentifier: identifier as String) {
        //do what you need with the event
    }