删除日历iOS中的重复条目

时间:2016-09-06 06:40:41

标签: ios objective-c

我试图将活动保存在原生日历中。但是,我的事件正在保存在日历中,但每次我在设备或模拟器上运行代码时,它都会创建重复的条目。我已经使用了所有必要的东西来避免它,但无法得到任何帮助。

这是我的代码。

-(void )addEvents :(NSMutableArray *)sentarray{

    for ( int i =0; i<sentarray.count; i++) {

        Schedule *schdeule = [events objectAtIndex:i];

        EKEventStore *store = [[EKEventStore alloc] init];

    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {


        dispatch_async(dispatch_get_main_queue(), ^{
            if (error)
            {
                NSLog(@"Error in dispatching data in the queue");
            }
            else  if (!granted) {

                NSLog(@"NoPermission to access the calendar");

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Cannot sync data with your calendar" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

                [alert show];

            return;

        }
            else{
                    EKEvent *event = [EKEvent eventWithEventStore:store];
                    event.title =schdeule.title;
                    event.startDate = schdeule.startDate; //today
                    event.endDate = schdeule.endDate;  //set 1 hour meeting
                    event.calendar = [store defaultCalendarForNewEvents];
                    NSError *err = nil;
                    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

                    // Store this so you can access this event later for editing
                    savedEventId = event.eventIdentifier;


        if (!err) {


            NSPredicate *predicateForEventsOnMeetingDate = [store predicateForEventsWithStartDate:schdeule.startDate endDate:schdeule.endDate calendars:nil]; // nil will search through all calendars

            NSArray *eventsOnMeetingDate = [store eventsMatchingPredicate:predicateForEventsOnMeetingDate];

            __block BOOL eventExists = NO;


            [eventsOnMeetingDate enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                for (EKEvent *eventToCheck in eventsOnMeetingDate) {
                    if ([eventToCheck.title isEqualToString:schdeule.title]) {
                        eventExists = YES;
                    }
                }

                if (eventExists == NO) {
                    EKEvent *addEvent = [EKEvent eventWithEventStore:store];
                    addEvent.title = schdeule.title;
                    addEvent.startDate = schdeule.startDate;
                    addEvent.endDate =schdeule.endDate;
                    [addEvent setCalendar:[store defaultCalendarForNewEvents]];
                    [store saveEvent:addEvent span:EKSpanThisEvent commit:YES error:nil];
                }






                 }];
                NSLog(@"saved");

                if (i == sentarray.count-1) {

                //  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"calshow://"]];


                        }

                }
        else   {

                NSLog(@"%@",[err localizedDescription]);

            }



        }


    });

}];


}

}

1 个答案:

答案 0 :(得分:0)

在eventExists = YES之后应用break;像

... // other code
for (EKEvent *eventToCheck in eventsOnMeetingDate) {
    if ([eventToCheck.title isEqualToString:schdeule.title]) {
        eventExists = YES;
        break;
    }
}
... //other code