NSURLConnection and multitasking in iOS
Asked Answered
C

2

7

I'm using NSURLConnection to download resources asynchronously in iOS. (They are large-ish PDF files, so it takes some time on a slow connection.)

Now I'm updating my app from iOS 3 to iOS 4. As my app is none of location-aware, voip, and background music, I guess I need to do something.

My question is, then, what happens to the NSURLConnection currently running? Is it suspended and magically resumed when the app comes back to the foreground, or is it outright killed? If it is the latter, what is the standard strategy to resume it automatically later? Is there a open-source subclass of NSURLConnection which automatically does that?

Criticize answered 28/11, 2010 at 15:8 Comment(1)
Do post your research on this topic, will be helpful for me and others :)Dusen
S
1

You can start a task that will run for at most 10 minutes. Look at using the beginBackgroundTaskWithExpirationHandler: API for this purpose. Just be aware, if your task takes too long, it will be killed by the OS.

Supernational answered 28/11, 2010 at 15:20 Comment(5)
That doesn't really answer the question... 'My question is, then, what happens to the NSURLConnection currently running? Is it suspended and magically resumed when the app comes back to the foreground, or is it outright killed?'Expositor
Actually it does. There are only 4 cases that ANY code survives being put into the background, and they are the three you mention, plus this one. You need to launch a background task at some point before your app starts transitioning states, to ensure the download continues to run in the background if the user exits. If you need to cancel the task, there's an API for that too.Supernational
What happens to the NSURLConnection after the app is resumed? Do I receive connection:didFail...: delegate message?Criticize
Then I guess the correct approach is to make a subclass of NSURLConnection which listens to the didEnterBackground notification...Criticize
That's not the correct approach. The correct approach is to launch your NSURLConnection inside the beginBackgroundTaskWithExpirationHandler: block.Supernational
G
1

The NSURLConnection is indeed suspended and started again when the app enters the foreground. Just make sure you kill the connection if the app moves from suspended to not running like so:

- (void)applicationWillTerminate:(UIApplication *)application {
    if (self.downloadConnection != nil){
        [self.downloadConnection cancel];
    }
}
Glimp answered 24/1, 2011 at 16:41 Comment(3)
Uhm, isn't the connection automatically killed when the app is terminated?Criticize
Yes when the app is terminated the connection is killed, but not when the app enters the background.Glimp
This is useless. 1. Apps that use multitasking (so all apps of today) don't use the applicationWillTerminate: method. 2. When the app closes why would you need to cancel the request if it is cancelled and released automatically is the app life cycle ends?? 3. Also NSURLConnections shouldn't be killed when the app enters background so the cancelapproach is wrong in every sense.Snoddy

© 2022 - 2024 — McMap. All rights reserved.