我的应用程序同步RSS提要大约需要15-30秒,每次同步我请求beginBackgroundTaskWithExpirationHandler:
。在iOS 7和iOS 8中,一切都运行得很好。
从iOS 9开始,调用[[UIApplication sharedApplication] endBackgroundTask: backgroundTask];
会导致应用程序崩溃,并出现debug消息:
由于信号9而终止。
根据我的研究,信号9意味着应用程序占用了太多内存。当我使用仪器时,应用程序从未超过30mb或40%的cpu。
我知道这是从调用endBackgroundTask:
,因为如果我不调用它,应用程序不会崩溃。然而,一旦我调用endBackgroundTask:应用程序每次都会崩溃。
我不知道这里出了什么问题。我什么都试过了。重写代码,移动代码,注释掉除了endBackgroundTask:之外的所有内容。任何帮助或见解将不胜感激。
代码如下:
@interface SyncClass ()
@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;
@end
-(void)startSync
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self beginBackgroundUpdateTask];
// I then call my syncing code [syncClass sync];
});
//When sync is done call endBackgroundTask
[self endBackgroundUpdateTask];
}
- (void) beginBackgroundUpdateTask
{
NSLog(@"Background Time:%f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
NSLog(@"Ending background task");
}
回答我自己的问题。AFNetworking和FMDB碰巧已经过时了。通过可可豆荚更新它们似乎解决了这个问题。