2016-11-18 78 views
4

我登錄(寫入文件),我的每一個應用程序從掛起模式喚醒的時間backgroundTimeRemaining值,之前,我開始的UIApplication的後臺任務與到期處理,像這樣(我的方法,這使得網絡請求中):爲什麼即使應用程序狀態爲UIApplicationStateBackground,backgroundTimeRemaining也會給出DBL_MAX?

當應用程序在前臺運行,在此屬性 值保持適當大:

if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){ 

     [Logger logIntoFileNamed:@"log" withContent:[NSString stringWithFormat:@" state %ld, background time remaining %.2f",(long)[[UIApplication sharedApplication] applicationState],[[UIApplication sharedApplication] backgroundTimeRemaining ]] andPrettyTime:true]; 

     UIApplication* app = [UIApplication sharedApplication]; 

     __block UIBackgroundTaskIdentifier task = [app beginBackgroundTaskWithExpirationHandler:^{ 

      [app endBackgroundTask:task]; 
      task = UIBackgroundTaskInvalid; 

      [Logger logIntoFileNamed:@"log" withContent:@" expiration handler executed " andPrettyTime:true]; 
     }]; 
    } 

根據文檔的backgroundTimeRemaining一個值,如果應用程序是在前臺大。

但這不是這裏的情況。由於應用程序狀態等於UIApplicationStateBackground,因此執行我的方法。 我在這裏錯過了什麼?

+0

剛剛檢查...我實際上'線程1'發生這種情況時。這應該是主線程(當我從'applicationDidEnterBackground'運行這段代碼時:'它說'線程5')。事情是,我不是編寫代碼這部分的人,我想,因爲我確信應用程序是從掛起模式喚醒的事實,我目前正在執行所有這些操作。 ..有更多的調查和代碼,但如果我在主線程,那麼我得到正常的結果,對吧? – Whirlwind

回答

0

是,根據文檔:

backgroundTimeRemaining的價值可大了,如果應用程序是在 前景。

在提供正確的backgroundTimeRemaining之前,applicationDidEnterBackground需要一段時間。這應該開始約180。在異步線程中使用dispatch_async運行它,並過濾掉前景值(> 180)。

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) 
    { 
     NSLog(@"Multitasking Supported"); 

     __block UIBackgroundTaskIdentifier background_task; 
     background_task = [application beginBackgroundTaskWithExpirationHandler:^ { 

      //Clean up code. Tell the system that we are done. 
      [application endBackgroundTask: background_task]; 
      background_task = UIBackgroundTaskInvalid; 
     }]; 

     //To make the code block asynchronous 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      //### background task starts 
      NSLog(@"Running in the background\n"); 
      while(TRUE) 
      { 
       //#### Filter Foreground Values 
       if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 180) { 
        NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]); 
        [NSThread sleepForTimeInterval:1]; //wait for 1 sec 
       } 

      } 

      //#### background task ends 
      //Clean up code. Tell the system that we are done. 
      [application endBackgroundTask: background_task]; 
      background_task = UIBackgroundTaskInvalid; 
     }); 
    } 
    else 
    { 
     NSLog(@"Multitasking Not Supported"); 
    } 

} 
+0

你有鏈接,這是記錄(關於狀態切換延遲)?另外我在執行任務時使用dispatch_async,因爲它是在另一個方法中調用的,所以我只留下了它。無論如何,我測試了一下,如果我將代碼隔離並將其放入applicationDidEnterBackground中的空項目中,那麼所有代碼​​都可以正常工作。問題是,這與我目前的設置不同。在應用程序從暫停模式中喚醒後,我運行此代碼。在這種情況下,我得到DBL_MAX值。否則,它給出的值<180. – Whirlwind

0

這可能是由於您記錄的方式backgroundTimeRemaining。嘗試投入後,你獲得後臺任務:

UIApplication *app = [UIApplication sharedApplication]; 
long appState = (long)app.applicationState; 

if (appState == UIApplicationStateBackground) { 
    __block UIBackgroundTaskIdentifier task = [app beginBackgroundTaskWithExpirationHandler:^{ 

    [app endBackgroundTask:task]; 
    task = UIBackgroundTaskInvalid; 
    [Logger logIntoFileNamed:@"log" withContent:@" expiration handler executed " andPrettyTime:true]; 
    }]; 

    NSTimeInterval backgroundTimeRemaining = app.backgroundTimeRemaining; 
    [Logger logIntoFileNamed:@"log" withContent:[NSString stringWithFormat:@" state %ld, background time remaining %.2f", appState, backgroundTimeRemaining] andPrettyTime:true]; 
} 
+0

實際上,嘗試過,iirc。但會再試一次。 – Whirlwind

相關問題