I'm a bit confuse of how to take advantage of the new iOS 7 NSURLSession
background transfers features and AFNetworking (versions 2 and 3).
I saw the WWDC 705 - What’s New in Foundation Networking
session, and they demonstrated background download that continues after the app terminated or even crashes.
This is done using the new API application:handleEventsForBackgroundURLSession:completionHandler:
and the fact that the session's delegate will eventually get the callbacks and can complete its task.
So I'm wondering how to use it with AFNetworking (if possible) to continue downloading in background.
The problem is, AFNetworking conveniently uses block based API to do all the requests, but if the app terminated or crashes those block are also gone. So how can I complete the task?
Or maybe I'm missing something here...
Let me explain what I mean:
For example my app is a photo messaging app, lets say that I have a PhotoMessage
object that represent one message and this object has properties like
state
- describe the state of the photo download.resourcePath
- the path to the final downloaded photo file.
So when I get a new message from the server, I create a new PhotoMessage
object, and start downloading its photo resource.
PhotoMessage *newPhotoMsg = [[PhotoMessage alloc] initWithInfoFromServer:info];
newPhotoMsg.state = kStateDownloading;
self.photoDownloadTask = [[BGSessionManager sharedManager] downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *filePath = // some file url
return filePath;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (!error) {
// update the PhotoMessage Object
newPhotoMsg.state = kStateDownloadFinished;
newPhotoMsg.resourcePath = filePath;
}
}];
[self.photoDownloadTask resume];
As you can see, I use the completion block to update that PhotoMessage
object according to the response I get.
How can I accomplish that with a background transfer? This completion block won't be called and as a result, I can't update the newPhotoMsg
.
AFURLSessionManager
AFURLSessionManager
creates and manages anNSURLSession
object based on a specifiedNSURLSessionConfiguration
object, which conforms to<NSURLSessionTaskDelegate>
,<NSURLSessionDataDelegate>
,<NSURLSessionDownloadDelegate>
, and<NSURLSessionDelegate>
.link to documentation here documentation
It shouldn't make any difference whether or not the callbacks are blocks or not. When you instantiate an
AFURLSessionManager
, make sure to instantiate it withNSURLSessionConfiguration backgroundSessionConfiguration:
. Also, make sure to call the manager'ssetDidFinishEventsForBackgroundURLSessionBlock
with your callback block - this is where you should write the code typically defined in NSURLSessionDelegate's method:URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
. This code should invoke your app delegate's background download completion handler.One word of advice regarding background download tasks - even when running in the foreground, their timeouts are ignored, meaning you could get "stuck" on a download that's not responding. This is not documented anywhere and drove me crazy for some time. The first suspect was AFNetworking but even after calling NSURLSession directly, the behaviour remained the same.
Good luck!
A couple of thoughts:
You have to make sure you do the necessary coding outlined in the Handling iOS Background Activity section of the URL Loading System Programming Guide says:
That guide shows some examples of what you can do. Frankly, I think the code samples discussed in the latter part of the WWDC 2013 video What’s New in Foundation Networking are even more clear.
The basic implementation of
AFURLSessionManager
will work in conjunction with background sessions if the app is merely suspended (you'll see your blocks called when the network tasks are done, assuming you've done the above). But as you guessed, any task-specific block parameters that are passed to theAFURLSessionManager
method where you create theNSURLSessionTask
for uploads and downloads are lost "if the app terminated or crashes."For background uploads, this is an annoyance (as your task-level informational progress and completion blocks you specified when creating the task will not get called). But if you employ the session-level renditions (e.g.
setTaskDidCompleteBlock
andsetTaskDidSendBodyDataBlock
), that will get called properly (assuming you always set these blocks when you re-instantiate the session manager).As it turns out, this issue of losing the blocks is actually more problematic for background downloads, but the solution there is very similar (do not use task-based block parameters, but rather use session-based blocks, such as
setDownloadTaskDidFinishDownloadingBlock
).An alternative, you could stick with default (non-background)
NSURLSession
, but make sure your app requests a little time to finish the upload if the user leaves the app while the task is in progress. For example, before you create yourNSURLSessionTask
, you can create aUIBackgroundTaskIdentifier
:But make sure that the completion block of the network task correctly informs iOS that it is complete:
This is not as powerful as a background
NSURLSession
(e.g., you have a limited amount of time available), but in some cases this can be useful.Update:
I thought I'd add a practical example of how to do background downloads using AFNetworking.
First define your background manager.
and
Make sure app delegate saves completion handler (instantiating the background session as necessary):
Then start your downloads:
Note, I don't supply any of those task related blocks, because those aren't reliable with background sessions. (Background downloads proceed even after the app is terminated and these blocks have long disappeared.) One must rely upon the session-level, easily recreated
setDownloadTaskDidFinishDownloadingBlock
only.Clearly this is a simple example (only one background session object; just saving files to the docs folder using last component of URL as the filename; etc.), but hopefully it illustrates the pattern.