• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 处理 EKEventStoreChangedNotification 通知

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:47:51 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在我的应用程序中列出事件。用户可以创建、编辑和删除事件。在 viewDidLoad 方法中,我获取我需要的所有事件并将它们推送到一个数组中。它按预期工作。

对于创建、编辑和删除事件,我使用 EKEventEditViewControllerEKEventViewController 效果很好。在 Controller 的委托(delegate)方法中,我对数组进行所需的更改并重新加载我的 View 。

当然,如果用户从另一个应用程序(如内置日历应用程序)进行一些更改,我也想知道并处理。所以我观察EKEventStoreChangedNotification。从该通知中,我只得到“发生了变化”,而不是哪个事件或来自哪个应用程序。实际上我想知道的是,我的应用程序或其他应用程序是否发生了更改,以及哪些事件已更改。由于我已经在 EKEventEditViewControllerDelegate 方法中处理了更改(来 self 的应用程序),因此我不需要再次处理它们。

如果我不知道哪些对象已更改,我必须获取并排序所有对象。

目前我在日历(开发设备)中只有 5 个事件,当然获取和排序所有事件不是问题,但如果用户有超过 1000 个,那么可能只有一个事件更改就过分了。

所以我的问题是:如何处理 EKEventStoreChangedNotification



Best Answer-推荐答案


您可以通过以下代码准确检测出哪个事件已被更改[免责声明代码不是我的想法,我在另一个 Stack Overflow 答案中找到并稍作修改]。

我正在使用一个名为“JSCalendarManager”的库与 eventstore 交互,在我的情况下,作为使用我的应用程序创建并与 iCalendar 同步的事件,我已经将它们的 eventIdentifier 保存在本地 DB 中,我可以检索我的时间绑定(bind)来搜索iCalendar 中的事件并匹配更改的事件。

+(void)iCloudStoreChangedNSNotification*)eventStoreChangeNotification{
NSArray* allScheduleRecords =[self getAllScheduleRecordSyncedToICalendar];

NSDate* startDate = [NSDate new];
NSDate* endDate   = [NSDate new];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat"yyyy-MM-dd HH:mm:ss"];

if (allScheduleRecords.count >= 2) {
    startDate = [dateFormatter dateFromString:[[allScheduleRecords firstObject] objectForKey"meetingTime"]];
    endDate = [dateFormatter dateFromString:[[allScheduleRecords lastObject] objectForKey"meetingTime"]];
}else if (allScheduleRecords.count > 0){
    startDate = [dateFormatter dateFromString:[[allScheduleRecords firstObject] objectForKey"meetingTime"]];

    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian componentsNSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:today];
    components.day = 1;
    endDate = [gregorian dateFromComponents:components];
}else{
}

NSArray *ekEventStoreChangedObjectIDArray = [eventStoreChangeNotification.userInfo objectForKey"EKEventStoreChangedObjectIDsUserInfoKey"];

[calendarManager findEventsBetween:startDate
                               and:endDate
                 withSearchHandler:^(BOOL found, NSError *error, NSArray *eventsArray) {
                     [eventsArray enumerateObjectsUsingBlock:^(EKEvent *ekEvent, NSUInteger idx, BOOL *stop) {
                         // Check this event against each ekObjectID in notification
                         [ekEventStoreChangedObjectIDArray enumerateObjectsUsingBlock:^(NSString *ekEventStoreChangedObjectID, NSUInteger idx, BOOL *stop) {
                             NSObject *ekObjectID = [(NSManagedObject *)ekEvent objectID];
                             if ([ekEventStoreChangedObjectID isEqual:ekObjectID]) {
                                 // Log the event we found and stop (each event should only exist once in store)
                                 NSLog(@"calendarChanged(): Event Changed: title:%@", ekEvent.title);
                                 [self updateAppointmentForEvent:ekEvent];
                                 *stop = YES;
                             }
                         }];
                     }];
                 }];}

关于ios - 处理 EKEventStoreChangedNotification 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17806209/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap