Networking pattern based on NSURLSession

NSExplorer

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.

  1. ViewController instantiates NSOperation subclass (which encapsulates URL, parameters etc)
  2. NSOperation subclass instanciates NSURLConnection (which performs synchronous request and retrieves data)
  3. NSURLConnection dumps data to NSOperation-subclass
  4. 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.

jonahb

You can use the same pattern, replacing NSURLConnection with NSURLSessionTask subclasses (e.g. NSURLSessionDataTask).

As @CouchDeveloper suggests in the comments, an alternative is to wrap NSURLSessionTask in an non-NSOperation object with asynchronous semantics (cancel, resume, etc.). This wrapper object would do little more than encode and decode parameters, delegating most operations to the wrapped task.

In either case, to instantiate NSURLSessionTask, you will need a NSURLSession. (NSURLSession is the NSURLSessionTask factory.) If all of your operations use the same configuration (cookies, proxy, caching, etc.), you can simply use the shared session (+[NSURLSession sharedSession]). If they need different configurations, you'll have to give them a NSURLSession or enough information to create their own.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Turn based networking in Unity

From Dev

Subnetting based on networking needs

From Dev

what is the advantage of using Alamofire over NSURLSession/NSURLConnection for networking?

From Dev

what is the advantage of using Alamofire over NSURLSession/NSURLConnection for networking?

From Dev

How to use NSURLSession and NSURLRequest to fetch users feeds and update in a tableview for a social networking iOS app?

From Dev

Trying to refactor networking method using Singleton pattern in iOS

From Java

Fill Down based on pattern

From Dev

Selecting columns based on pattern

From Dev

Grouping based on pattern Sql

From Dev

SQL Table based on a pattern

From Dev

Laravel pattern based filter

From Dev

rearrange the data based on a pattern

From Dev

PodSixNet multiplayer networking library for Python game based on Tkinter

From Dev

Networking, monitor and limits by data usage based on IP or user

From Dev

PodSixNet multiplayer networking library for Python game based on Tkinter

From Dev

Replace a pattern based off of the integer in the pattern in Vim

From Dev

Image size based on Forloop pattern

From Dev

Smarter string replace based on a pattern

From Dev

Remove folders based on naming pattern

From Dev

Skip tables in mysqldump based on a pattern

From Dev

R: list files based on pattern

From Dev

Add column based on pattern matching

From Dev

Regex to exclude matches based on pattern

From Dev

task based async pattern for wcf

From Dev

Weight based round robin pattern

From Dev

Splitting a string based on Pattern Java

From Dev

Pattern matching based on the function signature

From Dev

AWK print based on FILENAME pattern

From Dev

Situation based design pattern application

Related Related

HotTag

Archive