beginBackgroundTaskWithExpirationHandler, downloading data from server
<p>I did some research on the beginBackgroundTaskWithExpirationHandler and I don't think I am understanding it correctly.  I am under the impression that I can wrap a long task in it in the event that our app goes to the background, that task continues to get run.  So the first time you run our app, you can download more data from our server that provides the user with more information.  Since this can take awhile, I want to have this task run in the background and sleep mode (if possible).  </p>

<p>My original function without the beginBackgroundTaskWithExpirationHandler that gets called when the user clicks on download looks like:</p>

<pre><code>-(void) HandleDownload:(NSNotification *)notification
{
        DataManager *dmgr = [TestApp sharedDelegate].AppDataManager;
        NSString *nextPacketName = [dmgr GetNextPacketName];
        int totalNumberPackets = [dmgr GetRowCountForTable:@"Manifest"];
        if (nextPacketName == nil)
        { 
            [[NSNotificationCenter defaultCenter] removeObserver:self name:DownloadReferenceSequenceNotification object:nil];


            self.ProgressLabel.hidden = YES;
            self.ProgressBar.hidden = YES;
        }
        else
        {
            self.ProgressLabel.hidden = NO;
            self.ProgressLabel.text = [NSString stringWithFormat:@"Downloading %@", nextPacketName];
            self.ProgressBar.hidden = NO;
            self.ProgressBar.progress += 1. / (totalNumberPackets);

            WebServiceManager *wmgr = [WebServiceManager sharedInstance];
            [wmgr GetPacket:nextPacketName]; // This function creates the NSURL and starts the request
        }
}
</code></pre>

<p>This function works assuming the app is in the foreground.  What I tried doing to have this task be in the background is:</p>

<pre><code>-(void) HandleDownload:(NSNotification *)notification
    {
UIApplication *app = [UIApplication sharedApplication];
    __block UIBackgroundTaskIdentifier taskID;
    taskID = [app beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"Background task not completed");
        [app endBackgroundTask:taskID];

            DataManager *dmgr = [TestApp sharedDelegate].AppDataManager;
            NSString *nextPacketName = [dmgr GetNextPacketName];
            int totalNumberPackets = [dmgr GetRowCountForTable:@"Manifest"];
            if (nextPacketName == nil)
            { 
                [[NSNotificationCenter defaultCenter] removeObserver:self name:DownloadReferenceSequenceNotification object:nil];


                self.ProgressLabel.hidden = YES;
                self.ProgressBar.hidden = YES;
            }
            else
            {
                self.ProgressLabel.hidden = NO;
                self.ProgressLabel.text = [NSString stringWithFormat:@"Downloading %@", nextPacketName];
                self.ProgressBar.hidden = NO;
                self.ProgressBar.progress += 1. / (totalNumberPackets);

                WebServiceManager *wmgr = [WebServiceManager sharedInstance];
                [wmgr GetPacket:nextPacketName]; // This function creates the NSURL and starts the request
            }
                NSLog(@"Time remaining: %f", app.backgroundTimeRemaining);
    }];

    if (taskID == UIBackgroundTaskInvalid) {
        NSLog(@"Failed to create background task identifier");
    }
    }
</code></pre>

<p>Am I going about this the right way in trying to wrap my long running task in the beginBackgroundTaskWithExpirationHandler block?  It doesn't seem like I am since none of the code in the expiratinHandler block get called.  Thanks in advance!</p>
