2012-03-05 81 views
0

我正在開發一個應用程序,並且在用戶退出應用程序時遇到問題。 假設該應用程序正在下載3張圖片。 用戶退出應用程序。 如何繼續下載(因爲我們正在談論剩下的2個月,而不是500個!)? 我知道是這樣的:iOS - 如何在應用程序進入後臺之前完成任務

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

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

     NSLog(@"dispatch_async debut"); 

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

     [application endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 

     NSLog(@"dispatch_async fin"); 
    }); 
} 

的問題是,我的任務已經在處理。我不想重新開始下載!

你有什麼想法嗎?

非常感謝您的回答。

回答

0

的唯一方法是用

[NSData dataWithContentsOfURL:imageURL] 

這種方法下載同步方式中的數據是不是最好的。 你應該考慮一個不同的方法,即創建一個NSOperations隊列,並在進入後臺之前存儲隊列和隊列,這個隊列可以在應用程序返回前臺時簡單地恢復。

+0

好了,因此無法讓當前處理的任務完成?我必須停止並再次處理。 – Dabrut 2012-03-05 15:15:33

相關問題