2010-06-16 29 views
3

其實我工作的多任務處理和麪對錯誤,請幫助我,因爲我需要在後臺應用蘋果面對錯誤多任務處理代碼

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    UIApplication* app = [UIApplication sharedApplication]; 

    // Request permission to run in the background. Provide an 
    // expiration handler in case the task runs long. 
    NSAssert(bgTask == UIBackgroundTaskInvalid, nil); 

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     // Synchronize the cleanup call on the main thread in case 
     // the task actually finishes at around the same time. 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (bgTask != UIBackgroundTaskInvalid) 
      { 
       [app endBackgroundTask:bgTask]; 
       bgTask = UIBackgroundTaskInvalid; 
      } 
     }); 
    }]; 

    // Start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     // Do the work associated with the task. 

     // Synchronize the cleanup call on the main thread in case 
     // the expiration handler is fired at the same time. 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (bgTask != UIBackgroundTaskInvalid) 
      { 
       [app endBackgroundTask:bgTask]; 
       bgTask = UIBackgroundTaskInvalid; 
      } 
     }); 
    }); 
} 
+0

我相信你所要做的只會在iOS 4.0中提供,而iOS 4.0仍然處於NDA之下。最好在iPhone開發人員中心的開發人員論壇上試試你的運氣。 – 2010-06-16 23:48:25

回答

0

檢查工作,並在前臺應用了developer documentation,如果你有一個開發人員的帳戶。我們無法真正回答os4.0的問題,因爲它是在NDA之下,但您需要回答該問題的所有信息都在該頁面上(一旦登錄)。

+0

朋友我從指定的鏈接複製了這段代碼,但是在執行時遇到了錯誤,以及如何定義bgtask,因爲它顯示了bgtask不在結構或聯合中。 – user366584 2010-06-17 12:08:10

3

把你的@interface:

UIBackgroundTaskIdentifier bgTask; 

(出從現在NDA)。