长时间运行后台任务定期停止iOS
在我的应用程序中。长时间运行后台任务定期停止iOS
我使用以下代码以1分钟的时间间隔更新位置坐标到服务器。它在10到20小时内工作正常。但其一段时间停止定期请帮助我。
UIApplication *application1 = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier background_task;
background_task = [application1 beginBackgroundTaskWithExpirationHandler:^ {
[application1 endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//### background task starts
//NSLog(@"Running in the background\n");
while(TRUE)
{
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
NSUserDefaults *addValue=[NSUserDefaults standardUserDefaults];
NSString *oldLat=[addValue stringForKey:@"OLD_LAT"];
NSString *oldLong=[addValue stringForKey:@"OLD_LONG"];
CLLocationManager *manager = [[CLLocationManager alloc] init];
manager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
NSString *locLat = [NSString stringWithFormat:@"%lf",manager.location.coordinate.latitude];
NSString * locLong = [NSString stringWithFormat:@"%lf",manager.location.coordinate.longitude];
float lat_new=[locLat floatValue];
float lang_new=[locLong floatValue];
float lat_old=[oldLat floatValue];
float lang_old=[oldLong floatValue];
if (lat_new>0 && lang_new>0) {
//NSLog(@"location changed");
[addValue setObject:locLat forKey:@"OLD_LAT"];
[addValue setObject:locLong forKey:@"OLD_LONG"];
float from_lat_value=[locLat floatValue];
float from_long_value=[locLong floatValue];
locLat=[NSString stringWithFormat:@"%f",from_lat_value];
locLong=[NSString stringWithFormat:@"%f",from_long_value];
//NSLog(@"LST:%@,%@",locLat,locLong);
NSUserDefaults *addLat=[NSUserDefaults standardUserDefaults];
[addLat setObject:locLat forKey:@"FROM_LAT"];
[addLat setObject:locLong forKey:@"FROM_LONG"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/user/insertUserLocation",CONFIG_BASE_URL]];
// //NSLog(@"URL:%@",url);
__block ASIFormDataRequest *requestmethod = [ASIFormDataRequest requestWithURL:url];
NSUserDefaults *userValue=[NSUserDefaults standardUserDefaults];
NSString *deviceToken=[NSString stringWithFormat:@"%@",[userValue objectForKey:@"DEVICE_TOKEN"]];
NSString *loginValidation=[userValue objectForKey:@"USER_ID"];
[requestmethod setValidatesSecureCertificate:NO];
[requestmethod setPostValue:deviceToken forKey:@"deviceToken"];
[requestmethod setPostValue:locLat forKey:@"latitude"];
[requestmethod setPostValue:locLong forKey:@"longitude"];
[requestmethod setPostValue:loginValidation forKey:@"userID"];
[requestmethod setTimeOutSeconds:180];
[requestmethod setCompletionBlock:^{
NSString *responseString23 = [requestmethod responseString];
//NSLog(@"BACKGROUND RESPONCE:%@",responseString23);
}];
[requestmethod setFailedBlock:^{
NSError *error = [requestmethod error];
if ([[NSString stringWithFormat:@"%@",error.localizedDescription] isEqualToString:@"The request timed out"]||[[NSString stringWithFormat:@"%@",error.localizedDescription] isEqualToString:@"Please connect online to use the app"])
{
}
else
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Alert-360"
message:[NSString stringWithFormat:@"%@",error.localizedDescription] delegate:self
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
// [self endBackgroundUpdateTask];
}];
[requestmethod startAsynchronous];
}
[locationManager stopMonitoringSignificantLocationChanges];
[locationManager stopUpdatingLocation];
}
[NSThread sleepForTimeInterval:BACKGROUND_INTERVAL_CHECKIN]; //wait for 1 sec
//Clean up code. Tell the system that we are done.
[application1 endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
//NSLog(@"background Task finished");
});
检查我的表扬。
应用程序进入后台后,我们可以得到纬度和经度values.but我们不能在这个数据发布到服务器。 Apple限制了这一点。
-
我计算前景和背景剩余时间代码,我得到以下结果 前景: 前景剩余时间>>>>:179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000秒(2147483647分钟)
背景: 背景剩余时间>>> >:159.223682秒(2分钟)
对于我最后的表扬:我们无法长时间将后台位置更新运行到服务器。
我曾经收到同样的崩溃在后台运行我的GPS追踪器。我通过在每次调用之前检查值的有效性来修复这些崩溃问题。我发现我的一些变量是零。调用空变量的选择器很可能会在执行任务之外执行任务,而无需重新调度。
用每次任务调用的空检查来保护我的呼叫解决了我的问题。您还可以添加尽可能多的日志;并让它运行一段时间,然后通过检查日志来检查崩溃发生的位置。除了正常的nslog输出外,我还有一个外部共享文档记录文本文件。您应该能够将该文件中的所有应用程序日志在将来随时分离。确保在发送到市场之前关闭该注销。