在iOS中添加新事件之前检查重复的项目 - EKEventStore [英] Checking for duplicated items before adding new event in iOS - EKEventStore

查看:824
本文介绍了在iOS中添加新事件之前检查重复的项目 - EKEventStore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS编程的新手,我正在开发一个简单的项目,列出给定城市的假期,并允许用户将这些事件添加到iCal默认日历中。

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

问题是:如何检查用户日历中是否已存在具有相同属性(例如标题和开始日期)的事件。如果操作按钮(用于向iCal添加事件)被多次按下,则可能会发生这种情况。在这种情况下,我不想在iCal中创建两个或更多相同的事件。

The issue is: how to check if there is already an event with same properties (title and start date for example) in the user's calendar. This could happen if the action button (used to add an event to iCal) is pressed more than once. In such a situation, I don't want two or more identical events being created in iCal.

我试图使用NSPredicate,但我完全失去了如何获得它排序。

I have tried to use NSPredicate but I am totally lost on how to get it sorted.

任何帮助将不胜感激!
提前感谢。

Any help would come be appreciated! Thanks in advance.

Bellow是我的事件添加代码,只是为了清楚。在这种情况下,用户从列表中添加多个事件(例如所有本地假日)。

Bellow is my event-adding code just to make things clear. In this case a user is adding multiple events from a list (all local holidays for example).

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];
}   


推荐答案

摘要



在你的实例方法的某一点(可能在for循环),你将要创建一个基于 [allHolidayDates objectAtIndex:i] 返回一个循环的数组,以检查 [allHolidayNames objectAtIndex:i] 是否存在于返回的事件中。

Summary

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

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];
    }
}



提示




  • 为了帮助可视化数据,特别是数组和对象的内容,请尝试使用 NSLog

    NSLog(eventsOnHolidayDate =%@,eventsOnHolidayDate);

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

    请注意, eventsMatchingPredicate 阻止主线程,同时检索事件。如果您连续多次执行此操作,可能会影响用户体验。您应该考虑使用 enumerateEventsMatchingPredicate:usingBlock:(在此问题的范围之外)。

    Note that eventsMatchingPredicate will block the main thread whilst retrieving events. If your doing this multiple times in a row it could impact on the user experience. You should consider using enumerateEventsMatchingPredicate:usingBlock: (outside the scope of this question).

    这篇关于在iOS中添加新事件之前检查重复的项目 - EKEventStore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆