2

我已使用以下方式注册日历更改通知:

- (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 在看似随机的时间被调用,一些与日历完全无关。有没有办法找出触发后台获取的原因?它总是日历吗?实际执行被注释掉了——对吗?

我错过了什么?

4

1 回答 1

0

我一直在尝试自己找到这个答案,但我认为没有实际的方法可以做到这一点。根据 Apple 的文档,我们不允许在后台状态下访问系统资源:

在挂起之前停止使用共享系统资源。与通讯簿或日历数据库等共享系统资源交互的应用程序应在暂停之前停止使用这些资源。此类资源的优先级始终位于前台应用程序。当您的应用程序被挂起时,如果发现它正在使用共享资源,则该应用程序将被杀死。

我仍在寻找更好的答案/解决方法,但很想从其他人那里听到这个。

于 2014-08-29T18:19:02.257 回答