Running task in the background in case application closes
<p>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.</p>

<p>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. </p>

<p>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.</p>

<pre><code>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;
});
</code></pre>

<p>ANy idea how i can solve this problem?</p>
