Fetch events and reminders from the Calendar database.
To search for and return events and reminders from the Calendar database, you must connect to the event store, as discussed in "Accessing the Event Store."
There are two ways to retrieve events and reminders. You can fetch via:
Predicate, or search query, and return zero or more events that match a given query.
Unique identifier and return a single event that corresponds to the given identifier.
Search with Predicates
Predicates return events and reminders that match a search query.
It’s common to fetch events and reminders that fall within a date range. The
events(matching:): fetches all events that fall within the date range specified in the predicate you provide. The following listing demonstrates how to fetch all events that occur between one day before and one year after the current date.
You can specify a subset of calendars to search by passing an array of
EKCalendar objects as the calendars parameter of the
predicate method. You can get the user’s calendars from the event store’s
calendars(for:) method. Passing
nil tells the method to fetch from all of the user’s calendars.
events(matching:) method is synchronous, you may not want to run it on your app’s main thread. For asynchronous behavior, run the method on another thread with the
dispatch function or with an
You can call
fetch to access multiple reminders that match a predicate. Pass a predicate returned by one of the following methods:
predicatefinds incomplete reminders within an optional time period.
For Incomplete Reminders(with Due Date Starting: ending: calendars:)
predicatefinds completed reminders within an optional time period.
For Completed Reminders(with Completion Date Starting: ending: calendars:)
predicatefinds all reminders.
You can iterate across matched reminders by passing a block to the completion argument, as shown in Listing 2.
Unlike fetching events via predicate, you can fetch reminders via predicate asynchronously without dispatching to another thread.
Search with Unique Identifiers
If you know the event’s unique identifier because you fetched it previously with a predicate, you can use the
event(with to fetch the event. If it is a recurring event, this method will return the first occurrence of the event. You can get an event’s unique identifier with the
Similarly, if you know a specific reminder’s unique identifier from previously fetching it with a predicate, you can call the
calendar instance method.
calendar can fetch any calendar item (reminders and events), whereas
event(with fetches only events.