I've been traditionally using a pattern where NSOperation subclasses create and manage their own NSURLConnection. The NSOperation subclass is instantiated by the view controller and will do its work without bothering the controller until it has finished. When it finishes retrieving data, it executes the completion block supplied by the view controller.
- ViewController instantiates NSOperation subclass (which encapsulates URL, parameters etc)
- NSOperation subclass instanciates NSURLConnection (which performs synchronous request and retrieves data)
- NSURLConnection dumps data to NSOperation-subclass
- NSOperation-subclass executes the completion block supplied by the view controller.
I'm trying to implement the same pattern with NSURLSession now. I want to be able to encapsulate the url and parameters required to make a network request inside a single object. Do I achieve this using NSURLSession subclasses or NSURLSessionTask subclasses?
I like to create separate classes for every network operation based on the actor design pattern.
NSURLSessionTask
, the AFN class is not a subclass ofNSOperation
. Only if it uses an underlyingNSURLConnection
, the corresponding AFN class is a subclass ofNSOperation
. If you need a NSOperation subclass, you can't use AFN withNSURLSessionTask
. – CouchDeveloperNSURLSessionTask
, but you can wrap it in a thin object. – jonahb