在iOS7中使用后台获取的类Cron任务
我正试图在iOS7中实现一个长时间运行的类似cron的后台任务。我现在让它工作的方式是一个重复计时器,以少于10分钟的频率运行,然后以预定义的间隔启动和停止locationManager的startUpdatingLocation:在iOS7中使用后台获取的类Cron任务,ios,background,ios7,fetch,Ios,Background,Ios7,Fetch,我正试图在iOS7中实现一个长时间运行的类似cron的后台任务。我现在让它工作的方式是一个重复计时器,以少于10分钟的频率运行,然后以预定义的间隔启动和停止locationManager的startUpdatingLocation: /* * Very important, this function is called once a minute in the background and it specifies what actions to take based on
/*
* Very important, this function is called once a minute in the background and it specifies what actions to take based on the number of minutes passed.
*/
-(void)everyMinuteAction
{
NSLog(@"Every minute action");
if ([_killTime timeIntervalSinceNow] < 0.0)
{
//App is dead, long live the app!
_appKilledDueToInactivity = YES;
//Do some stuff to indicate to the user on the UI that they should not
}
//Once every n minutes, we need to turn on the GPS and report our location with four points.
NSLog(@"Modulus: %d", numberOfMinutesPassedSinceAppStarted % (int)floor((double)currentTTL/60.0));
if ((numberOfMinutesPassedSinceAppStarted % (int)floor((double)currentTTL/60.0) == 0) || numberOfMinutesPassedSinceAppStarted == 0)
{
//Treat it differently if we are in foreground or background
//GPS only responds with locations quickly when in foreground, so give it a bit more time if it is in the background.
if (inBackground)
{
[NSTimer scheduledTimerWithTimeInterval:140 target:self selector:@selector(killGPSAfterCertainTime) userInfo:nil repeats:NO];
} else {
//Start a timer that will kill the GPS after a certain period of time, regardless of how many points it has.
[NSTimer scheduledTimerWithTimeInterval:80 target:self selector:@selector(killGPSAfterCertainTime) userInfo:nil repeats:NO];
}
[self.locationManager startUpdatingLocation];
//Control is now passed on to didUpdateLocations, which will turn off location tracking after either a set period of time or a set number of
//locations received.
self.timeSpentFartassingAroundTryingToGetLocations = [[NSDate alloc] init];
self.numberOfLocationsCollectedThisTTL = 0;
}
if (3 % numberOfMinutesPassedSinceAppStarted == 0)
{
//Every 3 minutes we need to do some talking to the server
}
//Increment this, we've been using the app for another minute.
numberOfMinutesPassedSinceAppStarted += 1;
}
事实证明,如果TTL大于10分钟,它将在10分钟后被杀死,而且我也不完全确定它是否在plist文件中启用了后台位置权限的情况下工作
我想知道我是否能够使用新的fetchnetworkinformationbackground任务来实现这一点——只需更改方法的签名并每60秒注册一次该服务。根据过去的分钟数,我可以选择是通过网络查看一些信息,还是做一些有趣的生意
下一个问题是,在fetchapi上指定60秒的间隔是否真的能保证每60秒更新一次?还是会有明显的偏差?您在这里有进一步的了解吗?这种方法是客户强制要求的,但最终总是导致应用程序崩溃。我们采用了一种更加面向苹果的方法,对主要位置进行了更改,解决了这个问题。