2011-05-25 50 views
2

我正在創建一個應用程序,使用下拉框sdk從Dropbox下載大文件。下載功能的工作方式是我打電話給downloadFile方法,並將它傳遞給一個委託,在文件開始下載並完全下載文件後,它會回調它。在應用程序關閉的情況下在後臺運行任務

但是,現在,如果文件正在下載並關閉應用程序,則文件下載暫停,直到用戶重新進入應用程序。

我試過使用下面的代碼,但是當我關閉應用程序時,下載仍然沒有完成,直到你回到應用程序。

UIApplication* app = [UIApplication sharedApplication]; 

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
}]; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 


    //This call calls the sdk to start downloading the file. That method will then 
    // call this classes delegate methods with the progress of the download as well 
    // as when the file is totally finished downloading 
    [DBUtils downloadFile:fileVO.filename withHash:fileVO.filehash withRestClient:self.restClient]; 


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

任何想法如何我可以解決這個問題?

回答

1

我猜DBUtils downloadFile:...是一個異步方法。如果是這樣的話,你正在做的是開始下載,然後立即結束你的後臺任務。

你應該做的是爲DBUtils設置一個委託方法,讓你的類知道下載何時完成,然後從那裏調用endBackgroundTask。

+0

你完全正確!這工作!非常感謝!! – 2011-05-26 03:42:25

相關問題