长时间运行的后台任务定期停止iOS

时间:2014-09-29 07:01:31

标签: ios objective-c iphone location core-location

在我的申请表中。

我使用以下代码以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");

});

2 个答案:

答案 0 :(得分:0)

检查我的表扬。

  1. 应用程序进入后台后,我们可以获取纬度和经度值。但我们无法将此数据发布到服务器。 Apple限制了这一点。

  2. 我计算了前景和后台时间剩余代码,我得到了以下结果 前景: 其余&GT前景时间;>>计算值:179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000秒(2147483647分钟)

    背景: 剩余背景时间>>>>:159.223682秒(2分钟)

  3. 我最后的赞扬:我们无法长时间向服务器运行后台位置更新。

答案 1 :(得分:-1)

我曾经在后台运行的gps跟踪器中收到相同的崩溃。我通过在每次调用之前检查值的有效性来修复这些崩溃。我发现我的一些变量是零。调用空变量的选择器很可能会在任务函数之外执行而不重新安排。

通过对每个任务调用进行空检查来保护我的呼叫解决了我的问题。您也可以添加尽可能多的日志;并使其运行一段时间,然后通过检查日志来检查崩溃发生的位置。除了正常的nslog输出之外,我以前也有一个外部共享文档记录文本文件。您应该能够在将来的任何时间将该文件中的所有应用程序日志分开。确保在发送到市场之前关闭该注销。