I am trying to check if event already exists in calendar and show alert that it exists. This is my code below
Edited: It keeps adding Duplicate Events.
let event = EKEvent(eventStore: eventStore)
var savedEventId : String = ""
event.title = title
event.startDate = (self.dataEvent?.dates?.begin)!
event.endDate = (self.dataEvent?.dates?.end)!
event.calendar = eventStore.defaultCalendarForNewEvents
let predicate = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
let existingEvents = eventStore.eventsMatchingPredicate(predicate)
for singleEvent in existingEvents {
if singleEvent.title == self.dataEvent?.titleString && singleEvent.startDate == startDate && singleEvent.endDate == endDate {
let alert = UIAlertController(title: "Event Already Exists", message: "Event Already Exists in Calendar", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
do {
try eventStore.saveEvent(event, span: .ThisEvent)
savedEventId = event.eventIdentifier
print("Event Added")
let alert = UIAlertController(title: "Event Successfully Added", message: "Event Added to Calendar", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} catch {
print("Error occurred")
}
The problem is that your do-catch
block in which you add the event gets called all the time even if the event exists. You need to create a boolean to track whether the same event was found or not and only execute the do-catch
block if it wasn't found.
Instead of using a for loop, you can use Array.contains(where:)
, which allows for an early exit if such an event was found.
var eventAlreadyExists = false
let event = EKEvent(eventStore: eventStore)
var savedEventId : String = ""
event.title = title
event.startDate = (self.dataEvent?.dates?.begin)!
event.endDate = (self.dataEvent?.dates?.end)!
event.calendar = eventStore.defaultCalendarForNewEvents
let predicate = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
let existingEvents = eventStore.eventsMatchingPredicate(predicate)
let eventAlreadyExists = existingEvents.contains(where: {event in self.dataEvent?.titleString == event.title && event.startDate == startDate && event.endDate = endDate})
// Matching event found, don't add it again, just display alert
if eventAlreadyExists {
let alert = UIAlertController(title: "Event Already Exists", message: "Event Already Exists in Calendar", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
// Event doesn't exist yet, add it to calendar
do {
try eventStore.saveEvent(event, span: .ThisEvent)
savedEventId = event.eventIdentifier
print("Event Added")
let alert = UIAlertController(title: "Event Successfully Added", message: "Event Added to Calendar", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} catch {
print("Error occurred")
}
}
Note: If your endDate and startDate are equal - then use in predicate range of time, eg endDate + 1, startDate +1 in eventStore.predicateForEventsWithStartDate. Overwise your existingEvents will always empty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With