Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does AFNetworking 2.0 Supports Background task? - iOS 7

i am using Afnetworking 2.0 library with NSURLSession.

i found in AFURLSessionManager they configure Session with default session , so if i need to download images in background then i have to set Session with Background configuration.

So , I have to change AFNetworking library for that or is there any other way for that in AFNetworking 2.0.

like image 700
PJR Avatar asked Nov 18 '13 11:11

PJR


1 Answers

From Using NSURLSession:

The NSURLSession class supports background transfers while your app is suspended. Background transfers are provided only by sessions created using a background session configuration object (as returned by a call to backgroundSessionConfiguration:).

You must configure your AFHTTPSessionManager to use the background session configuration if you want to do this:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.myApp.backgroundDownloadSession"]
AFHTTPSessionManager *backgroundManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];

AFNetworking will set itself as the delegate. From the NSURLSession docs:

[T]he delegate will be retained until after the delegate has been sent the URLSession:didBecomeInvalidWithError: message

As a result, your manager will stick around as long as this session does.

Two side notes:

  1. You should probably use use a separate AFHTTPSessionManager for background transfers (large downloads, etc.) You don't want literally all requests to be assigned a background URL session.

  2. In case you want to retrieve the response without AFNetworking, note what the background session identifier is ('com.myApp.backgroundDownloadSession' in my sample code):

    An identifier for the new session configuration that is unique for your app. Your app can retrieve the download or the upload response later by creating a new background session with the same identifier.

like image 139
Aaron Brager Avatar answered Sep 21 '22 12:09

Aaron Brager