For Swift 3
I found out that the problem was that I created the repository in a function that retrieves the date.
Creating storage outside the function and using its instance solved the problem.
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) } } } }
source share