views:

220

answers:

1

Hi,

Using iOS 4.0 I am trying to do a download an image in the background when the app is suspended. What i am doing is when i get the applicationDidEnterBackground delegate call, i initiate one asynchronous NSURLConnection and set the app delegate as the delegate for the connection. But none of the NSURLConnection delegates are getting called back. I have captured the network calls using wireshark and i can see that my request succeeded and got the response too. But since none of the delegate methods are invoked i am unable to do anything with the data.

- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
// UIBackgroundTaskIdentifier bgTask is instance variable
NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;
    });
}];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://animal.discovery.com/mammals/leopard/pictures/leopard-picture.jpg"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
[self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[self.connection start];
[request release];
NSLog(@"Sent download request....");
dispatch_async(dispatch_get_main_queue(), ^{
    while([application backgroundTimeRemaining] > 1.0) {
        //Do something..
    }
    [application endBackgroundTask:self->bgTask];
    self->bgTask = UIBackgroundTaskInvalid;
    NSLog(@"Ending background task....");
});

}

What should I do to complete a download asynchronously in background when the application goes to background?

Thanks,

Tony

A: 

Hi, I have the same issue... Ill try to terminate the async connection and use a synchronous one instead.. dont know if it works ;)

Dominik