2011-05-25 113 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

相关问题