Check for duplicate items before adding a new event in iOS - EKEventStore

I'm new to iOS programming, and I'm working on a simple project that lists holidays from a given city and gives users the ability to add these events to the iCal default calendar.

The problem is how to check if the user’s calendar already has the same property (name and start date). This can happen if the action button (used to add an event to iCal) is pressed more than once. In this situation, I do not want to create two or more identical events in iCal.

I tried using NSPredicate, but I completely lost the sorting method.

Any help would be appreciated! Thanks in advance.

Bellow is my event adding code to make things clear. In this case, the user adds several events from the list (for example, all local holidays).

for (int i = 0; i<[allHolidayNames count]; ++i) { // ------ EVENT MANIPULATION ------ EKEventStore *eventStore = [[EKEventStore alloc] init]; EKEvent *addEvent = [EKEvent eventWithEventStore:eventStore]; addEvent.title = [allHolidayNames objectAtIndex:i]; addEvent.startDate = [allHolidayDates objectAtIndex:i]; addEvent.allDay = YES; [addEvent setCalendar:[eventStore defaultCalendarForNewEvents]]; [eventStore saveEvent:addEvent span:EKSpanThisEvent commit:YES error:nil]; } 
+6
source share
1 answer

Summary

At some point in your instance method (perhaps during the for loop), you will want to create an NSPredicate based on [allHolidayDates objectAtIndex:i] to return the array you are using to check if [allHolidayNames objectAtIndex:i] is present in the returned events.

Code example

 for (int i = 0; i<[allHolidayNames count]; ++i) { // ------ EVENT MANIPULATION ------ EKEventStore *eventStore = [[EKEventStore alloc] init]; NSPredicate *predicateForEventsOnHolidayDate = [eventStore predicateForEventsWithStartDate:[allHolidayDates objectAtIndex:i] endDate:[allHolidayDates objectAtIndex:i] calendars:nil]; // nil will search through all calendars NSArray *eventsOnHolidayDate = [eventStore eventsMatchingPredicate:predicateForEventsOnHolidayDate] BOOL eventExists = NO; for (EKEvent *eventToCheck in eventsOnHolidayDate) { if ([eventToCheck.title isEqualToString:[allHolidayNames objectAtIndex:i]]) { eventExists = YES; } } if (eventExists == NO) { EKEvent *addEvent = [EKEvent eventWithEventStore:eventStore]; addEvent.title = [allHolidayNames objectAtIndex:i]; addEvent.startDate = [allHolidayDates objectAtIndex:i]; addEvent.allDay = YES; [addEvent setCalendar:[eventStore defaultCalendarForNewEvents]]; [eventStore saveEvent:addEvent span:EKSpanThisEvent commit:YES error:nil]; } } 

Advice

  • To visualize data, especially the contents of arrays and objects, try using NSLog . This will print the contents of the object to the console in order to better understand the data structures.

    NSLog ("eventsOnHolidayDate =% @", eventsOnHolidayDate);

  • Note that eventsMatchingPredicate will block the main thread during event extraction. If you do this several times in a row, it can affect the user's experience. You should consider using enumerateEventsMatchingPredicate:usingBlock: (outside the scope of this question).

+15
source

Source: https://habr.com/ru/post/908739/


All Articles