我正在做一个xcode-5的应用程序,我需要在后台模式下上传一些数据,我需要一组重复的代码,直到设备终止后台进程。我可以做一次,但我需要一次又一次地重复,以便我可以检查连接并执行上传。当应用程序进入活动模式时,我也会做同样的事情。目前,我使用以下代码的计时器:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[bgrdTimer_ActiveMode invalidate];
bgrdTimer_ActiveMode=nil;
self.backgroundTask=[application beginBackgroundTaskWithExpirationHandler:^{
self.backgroundTask=UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
bgrdTimer_BackGroundMode=[NSTimer scheduledTimerWithTimeInterval:40.00 target:self selector:@selector(repeatUploadProcess_BackGroundMode) userInfo:nil repeats:YES];
});
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[bgrdTimer_BackGroundMode invalidate];
bgrdTimer_BackGroundMode=nil;
bgrdTimer_ActiveMode=[NSTimer scheduledTimerWithTimeInterval:40.00 target:self selector:@selector(repeatUploadProcess_ActiveMode) userInfo:nil repeats:YES];
}repeatUploadProcess_ActiveMode和repeatUploadProcess_BackGroundMode是包含假定要重复的代码集的方法
问题是,当我使bgrdTimer_ActiveMode无效时,其他计时器不会被调用。
发布于 2014-11-21 01:14:31
你开启后台抓取模式了吗?单击项目目标,并在capabilities选项卡下选择"background fetch“。
理论上,您还应该设置应用程序endBackgroundTask:bgTask。听起来你知道,但如果它永远不会结束,iOS最终会杀死你的应用程序。
https://stackoverflow.com/questions/27039921
复制相似问题