在我的應用程序中。長時間運行後臺任務定期停止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");
});