I am creating an application where i am downloading a large file from dropbox using the drop box sdk. The way the download function works is i make a call to the downloadFile method and pass it a delegate where it will call back as the file starts downloading and after the file fully downloads.
However, right now if a file is downloading and i close the application, the file download pauses until the user goes back into the app.
I have tried using the following code but when i close the app, the download still does not finish until you go back into the app.
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;
});
ANy idea how i can solve this problem?
I’m guessing DBUtils downloadFile:… is an asynchronous method. If that’s the case, what you’re doing is starting the download then immediately ending your background task.
What you should be doing is setting yourself a delegate method on DBUtils, to let your class know when the download is finished, then calling endBackgroundTask from there.