2011-11-01 14 views
0

如果我們有一個長期運行的任務,我們可以確保應用程序不會退出,直到任務是通過使用下面的代碼完成:多beginBackgroundTaskWithExpirationHandler

UIApplication *app = [UIApplication sharedApplication];  

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    // Clean up any unfinished task business by marking where you. 
    // stopped or ending the task outright. 
    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
}]; 

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

    // Do the work associated with the task, preferably in chunks. 

    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
}); 

在我的情況下,應用程序的plist包含設置爲YES的條目Application does not run in background。現在,我有一個串行隊列(dispatch_queue_create(..., NULL)),它記錄了操作並逐一執行它們。用戶可以一次發佈多個任務,並且可能需要幾秒鐘20-30才能完成所有任務。

現在我的問題是,我可以使用beginBackgroundTaskWithExpirationHandler與我的串行隊列?如果是這樣,有什麼建議如何?我相信我必須保持一系列UIBackgroundTaskIdentifier

回答

0

據我所知,如果應用程序不支持後臺處理,應用程序會立即退出。所以,開始後臺任務是不可能的。

相關問題