26

I'm in the process of investigating AFNetworking as a replacement for ASIHTTPRequest, and notice a complete lack of information on whether it supports background downloads/uploads.

With an ASIHTTPReqeust object, all you have to do is call [request setShouldContinueWhenAppEntersBackground:YES] and the request will continue in the background. Is there any support for this in AFNetworking?

phi
  • 10,634
  • 6
  • 53
  • 88
Andy Riordan
  • 1,154
  • 1
  • 8
  • 13
  • If there isn't (I'm not sure whether there *is* support for background execution in `AFNetworking`), you could always port across whatever extra features you want from `AFNetworking` into `ASIHTTPRequest`, or add background execution support (relatively simple) to `AFNetworking`. –  Oct 17 '11 at 23:09

1 Answers1

48

EDIT: As of AFNetworking 1.0RC1, this is an explicit feature. AFURLConnectionOperation now has the method setShouldExecuteAsBackgroundTaskWithExpirationHandler:, which transparently manages all of this for you.


It's an implicit feature, so I didn't really think about advertising it. All you'd need to do is:


- (void)applicationWillResignActive:(UIApplication *)application {
    __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
        [application endBackgroundTask:backgroundTaskIdentifier];
        [[YourRestClient sharedClient] cancelAllHTTPOperations];
    }];
}

Or, if you manage your operations in your own NSOperationQueue, just -cancelAllOperations here instead.

Luke Mcneice
  • 3,012
  • 4
  • 38
  • 50
mattt
  • 19,544
  • 7
  • 73
  • 84
  • 5
    Mattt - sounds like you're giving a solution for canceling networking when moving into the background though the question was about spawning/continuing AFNetworking ops when moving into the background. Thoughts on that? Given that most AF methods are block based / return immediately, I've found that the system believes they are complete before they really run their course... Maybe just using waitUntilFinished when running in a background mode? – Hunter Nov 07 '11 at 01:13
  • 8
    @Hunter I think, you understand it wrong. Calling beginBackgroundTaskWithExpirationHandler ensures that requests continue in the background. The requests will not be canceled until the app is inactive for a longer time (like 10 min). – Felix Nov 18 '11 at 13:45
  • Yeah, you're completely right. I don't know what I was thinking when I wrote the above. – Hunter Nov 19 '11 at 16:55
  • 7
    WARNING: do not use mattt's beginBackgroundTaskWithExpirationHandler code on applicationWillResignActive WITHOUT ADDING a matching call to endBackgroundTask - otherwise IOS will kill your app after 600 seconds, and foregrounding will relaunch your app. – mr_marc Jan 25 '12 at 00:04
  • 3
    `- (void)applicationWillResignActive:(UIApplication *)application { UIBackgroundTaskIdentifier backgroundIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) { [[AFHTTPClient sharedClient] cancelAllHTTPOperations]; }]; [application endBackgroundTask:backgroundIdentifier]; }` – mr_marc Jan 25 '12 at 00:06
  • @mattt, I'm only now looking into AFNetworking, but it seems from your example that you can only do backgrounding on a global level, in contrast to ASIHTTPRequest's ability to specify which requests to background. Is this correct? – poetmountain Mar 09 '12 at 22:12
  • @BourbonJockey This will [likely be added to the next release](https://github.com/AFNetworking/AFNetworking/pull/259) (1.0) – mattt Mar 20 '12 at 18:44
  • 7
    @mattt you might want to update your answer to mention the `setShouldExecuteAsBackgroundTaskWithExpirationHandler:` you've added. I didn't realize it was there in the version I downloaded a couple of weeks ago. – brainjam May 18 '12 at 20:54
  • You should add `__block` modifier to `UIBackgroundTaskIdentifier backgroundTaskIdentifier`, or the block will capture its initialized value. – Vadim Yelagin May 21 '12 at 05:21
  • @mattt I simply love AFNetworking but in a "legacy" project I'm using a `NSOperationQueue` object. I have a question for you and if you can reply I would be grateful. Within the `main` method of a `NSOperation` subclass, I download a from a service through `+sendSynchronousRequest:returningResponse:error`. Once dowloaded, the file is imported within core data. A cancel request can happen at any time. So, does the cancellation could be handled in the right way or do I need to use aync pattern to deal with cancellation request? Thanks for your support. – Lorenzo B Nov 14 '12 at 22:19
  • What is the `[[YourRestClient sharedClient] cancelAllHTTPOperations];` line referring to? – HighFlyingFantasy Mar 13 '13 at 01:11
  • cancelAllHTTPOperations seems not to be included any longer in the AFHTTPClient class. Instead use [[[YourHTTPClient sharedClient] operationQueue] cancelAllOperations]; – Diego Jun 10 '13 at 13:49