在 iOS 中添加新事件之前检查重复项目 - EKEventStore
我是 iOS 编程新手,正在开发一个简单的项目,该项目列出了给定城市的假期,并让用户能够将这些事件添加到 iCal 默认日历中。
问题是:如何检查用户日历中是否已存在具有相同属性(例如标题和开始日期)的事件。如果多次按下操作按钮(用于向 iCal 添加事件),则可能会发生这种情况。在这种情况下,我不希望在 iCal 中创建两个或多个相同的事件。
我尝试过使用 NSPredicate 但我完全不知道如何对其进行排序。
任何帮助将不胜感激! 提前致谢。
下面是我的事件添加代码,只是为了让事情变得清楚。在这种情况下,用户从列表中添加多个事件(例如所有当地节假日)。
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];
}
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.
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.
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 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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
摘要
在实例方法中的某个时刻(可能在 for 循环期间),您将希望基于
[allHolidayDates objectAtIndex:i]
创建一个 NSPredicate 以返回一个数组,您循环遍历该数组以检查是否[allHolidayNames objectAtIndex:i]
存在于返回的事件中。示例代码
提示
为了帮助可视化数据,尤其是数组和对象的内容,请尝试使用
NSLog
。这会将对象的内容输出到控制台,以帮助您更好地理解数据结构。NSLog("eventsOnHolidayDate = %@",eventsOnHolidayDate);
请注意,
eventsMatchingPredicate
将在检索事件时阻止主线程。如果您连续多次执行此操作,可能会影响用户体验。您应该考虑使用enumerateEventsMatchingPredicate:usingBlock:
(超出了本问题的范围)。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.Example code
Tips
To help visualise the data, especially the contents of arrays and objects, try using
NSLog
. This will output the contents of an object to the console to help you understand the data structures a bit better.NSLog("eventsOnHolidayDate = %@",eventsOnHolidayDate);
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 usingenumerateEventsMatchingPredicate:usingBlock:
(outside the scope of this question).