2014-09-29 143 views
0

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

}); 

回答

0

檢查我的表揚。

  1. 應用程序進入後臺後,我們可以得到緯度和經度values.but我們不能在這個數據發佈到服務器。 Apple限制了這一點。

  2. 我計算前景和背景剩餘時間代碼,我得到以下結果 前景: 前景剩餘時間>>>>:179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000秒(2147483647分鐘)

    背景: 背景剩餘時間>>> >:159.223682秒(2分鐘)

  3. 對於我最後的表揚:我們無法長時間將後臺位置更新運行到服務器。

-1

我曾經收到同樣的崩潰在後臺運行我的GPS追蹤器。我通過在每次調用之前檢查值的有效性來修復這些崩潰問題。我發現我的一些變量是零。調用空變量的選擇器很可能會在執行任務之外執行任務,而無需重新調度。

用每次任務調用的空檢查來保護我的呼叫解決了我的問題。您還可以添加儘可能多的日誌;並讓它運行一段時間,然後通過檢查日誌來檢查崩潰發生的位置。除了正常的nslog輸出外,我還有一個外部共享文檔記錄文本文件。您應該能夠將該文件中的所有應用程序日誌在將來隨時分離。確保在發送到市場之前關閉該註銷。