Does AFNetworking 2.0 support a background job? - iOS 7

I am using Afnetworking 2.0 library with NSURLSession.

i found in AFURLSessionManager, they set up a session with a default session, so if I need to load images in the background, I have to set the Session with Background setting.

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

+7
ios objective-c ios7 afnetworking afnetworking-2
source share
1 answer

From Using NSURLSession :

The NSURLSession class supports background hyphenation while your application is paused. Background transfers are provided only by sessions created using the background session configuration object (which is returned by calling 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 act as a delegate. From NSURLSession docs:

[T] The delegate will be saved until the delegate receives the message URLSession:didBecomeInvalidWithError:

As a result, your manager will stick to this session.

Two side notes:

  • You should probably use a separate AFHTTPSessionManager for background transfers (large downloads, etc.). You do not want literally all requests to have a background URL session assigned.

  • If you want a response without AFNetworking, note that the background session identifier ('com.myApp.backgroundDownloadSession' in my code example):

    The identifier for the new session configuration that is unique to your application. Your application may receive a download or download response later by creating a new background session with the same identifier.

+3
source share

All Articles