0

I'm developing an App and I'm facing a problem when users quit the app. Let's say the app is downloading 3 images. The user quits the app. How can I continue the download (because we're talking about 2 mo left and not 500 !)? I know something like this :

if ([_downloader isDownloading]) {
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        // Clean up any unfinished task business by marking where you.
        // stopped or ending the task outright.
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSLog(@"dispatch_async debut");

        // Do the work associated with the task, preferably in chunks.

        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;

        NSLog(@"dispatch_async fin");
    });
}

The problem is that my task is already processing. I don't want to start again the download !

Do you have any ideas ?

Thanks a lot for your answers.

Dabrut
  • 862
  • 1
  • 10
  • 25

2 Answers2

1

I would advise using ASIHTTPRequest that has a built in option for resuming active downloads. http://allseeing-i.com/ASIHTTPRequest/

Stavash
  • 14,244
  • 5
  • 52
  • 80
0

The only way is to download the data in a synchronous way with

[NSData dataWithContentsOfURL:imageURL]

This approach is not the best. You should consider a differente approach that is to create a queue of NSOperations and, before go in background, store the queue and kill the queue, queue that you can simply restore once the app returns in foreground.

bontoJR
  • 6,995
  • 1
  • 26
  • 40
  • Ok so there is no way to let the task currently processing finish? I'll have to stop it and then process again. – Dabrut Mar 05 '12 at 15:15