1

I receive some events from a webservice. I add these events programmatically using Eventkit. I have a Add button which invokes code to add received events. However, repeated add action duplicates events rather than merging (in case event already exists).

How to avoid duplication of events?

Amar
  • 13,202
  • 7
  • 53
  • 71
  • when you are clicking add events button, you need to check for event already exists and then you need to insert it. you can check the events using their unique identifiers. – RAJA Mar 13 '14 at 06:12
  • [this](http://stackoverflow.com/q/6077613/2389078) and [that](http://stackoverflow.com/q/19496772/2389078) might be helpful. – DroidDev Mar 13 '14 at 06:20
  • Show your code to add events. – Amar Mar 13 '14 at 06:25

2 Answers2

5

You have to check the existing events before adding it. The below code shows how to do that. I have checked title and start date alone to identify an event, you can use more parameters as per need.

EKEventStore *eventStore = [[EKEventStore alloc] init];
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:[startDate dateByAddingTimeInterval:-60]
                                                             endDate:[endDate dateByAddingTimeInterval:60] calendars:nil];
    NSArray *events = [eventStore eventsMatchingPredicate:predicate];
    bool found = NO;
    for ( EKEvent *evt in events )
    {
        if ( [evt.title isEqualToString:title]  && [evt.startDate isEqualToDate:startDate])
        {
            found = YES;                
            break;
        }
    }
if(!found)
{
//Add your event here.
}
Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • It gives this error- Predicate call to calendar daemon failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)" – Puneetpal Arneja Mar 13 '14 at 11:23
  • The error means your application do not have permission to access calendar. You can enable it in the iPhone settings for your app. – Vignesh Mar 18 '14 at 07:29
0

Even i faced duplicate events issue, while adding events to the calendar. So before adding the events, i removed them first by passing start and end date in predicate as below.

    func removeMobileCalendarEvents(){
    //lstEvents is response array which i got from web service and stored in model class
    for eventObj in lstEvents.list {

        let predicate = eventStore.predicateForEvents(withStart: eventStartDate, end: eventEndDate, calendars: nil)

        let events = eventStore.events(matching: predicate)

        for event in events {
            print("event title",event.title)
            print("event start date",event.startDate)
            print("event end date",event.endDate)
            //lstEvents is response array which i got from web service and stored in model class
            for eventObj in lstEvents.list {
                if event.title! == eventObj.EventName { 
                    //exists

                    do {
                        try eventStore.remove(event, span: .thisEvent, commit: true)
                    }
                    catch {
                        print("Error saving event in calendar")             }

                }else{
                    //not exists
                }
            }

        }
    }

    addingEventsToMobileCalendar()

}

Call removeMobileCalendarEvents() method once you receive response from web service. Call removeMobileCalendarEvents() method, those many times the api is getting called.

Arshad Shaik
  • 1,095
  • 12
  • 18