我已使用以下方式注册日历更改通知:
- (void) registerForLocalCalendarChanges
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localCalendarStoreChanged) name:EKEventStoreChangedNotification object:self.store ];
}
当对本地日历进行更改时,这应该调用以下命令:
- (void) localCalendarStoreChanged
{
// This gets called when an event in store changes
// you have to go through the calendar to look for changes
// launch this in a thread of its own!
ashsysCalendarEventReporter *eventReport = [ashsysCalendarEventReporter new];
NSLog(@"Local Calendar Store Changed");
[NSThread detachNewThreadSelector:@selector(getCalendarEvents) toTarget:eventReport withObject:nil];
}
但是......当我启动应用程序,然后将其发送到后台以便我可以更改日历条目时,当我更改日历条目时没有任何反应。当我返回应用程序时它会触发。但是,这当然不是目标。
store 在头文件中定义:
@property (strong,nonatomic) EKEventStore *store;
更新...忘记显示我在后台获取的东西。
这是在 didFinishLaunchingWithOptions
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
这是在应用程序委托中:
- (void) application:(UIApplication*) application performFetchWithCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler
{
// UIBackgroundTaskIdentifier uploadCalInfo = [application beginBackgroundTaskWithExpirationHandler:nil];
NSLog(@"A fetch got called");
// ashsysCalendarEventReporter *eventReport = [ashsysCalendarEventReporter new];
// [eventReport getCalendarEvents];
// // [NSThread detachNewThreadSelector:@selector(getCalendarEvents) toTarget:eventReport withObject:nil];
// [application endBackgroundTask:uploadCalInfo];
completionHandler(UIBackgroundFetchResultNewData);
performFetch 在看似随机的时间被调用,一些与日历完全无关。有没有办法找出触发后台获取的原因?它总是日历吗?实际执行被注释掉了——对吗?
我错过了什么?