【问题标题】:Long running Background task Stopped Periodically iOS长时间运行的后台任务定期停止iOS
【发布时间】:2014-09-29 07:01:30
【问题描述】:

在我的应用程序中。

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

});

【问题讨论】:

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


    【解决方案1】:

    检查我的赞扬。

    1. App进入后台后,我们可以获取纬度和经度值。但是我们不能将这些数据发布到服务器。苹果对此进行了限制。

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

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

    3. 我最后的表扬:我们不能长时间运行后台位置更新到服务器。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-26
        • 2018-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        相关资源
        最近更新 更多