Part1:
From a developer ' s perspective, one of the most significant changes in IOS 7, and OS X Mavericks for that matter, is the I Ntroduction of Nsurlsession. Even thoughnsurlsession may seem daunting at a-glance, it's important that you understand what it are, how it relates t o Nsurlconnection, and what the differences are. In this series, I'll take you through the fundamentals of nsurlsession so you can take advantage of this new technology In your own applications.
From a developer's perspective, the most significant change to iOS7 and OS X Mavericks is the introduction of nsurlsession. Although at first glance, Nsurlsession is a bit intimidating, it is important to understand what it is and how it differs from nsurlconnection. In this series of networking with nsurlsession tutorials, I'll take you through the basics of nsurlsession, and you can also use the advantages of this new technology in your applications. Why Replace nsurlconnection?
The "I question you" asking yourself is why Apple found it necessary to introduce nsurlsession while we are PERFE Ctly happy with Nsurlconnection. The real question is whether your are happy with nsurlconnection. Do you remember this time your were cursing and throwing things at nsurlconnection? Most Cocoa developers have been in ' where ', which is why Apple decided-go-back to the drawing board and create a MOR e elegant solution, better suited for the modern web.
You may ask a question: why is it necessary to introduce nsurlsession when we are happy to use nsurlconnection? The question, however, is whether you are really happy with nsurlconnection. Did you forget that you used to curse the nsurlconnection, yes, most COCOA developers have had this experience, so Apple decided to redesign a more elegant solution to adapt to modern web development.
Even though Nsurlsession and nsurlconnection have a lot in common into terms of how to they work, at a fundamental level, they are quite different. Apple created Nsurlsessionto resemble the general concepts of nsurlconnection, but your'll learn in the course to this SE Ries that nsurlsession are modern, easier to use, and built with the mobile in mind.
Although Nsurlsession and nsurlconnection have a lot in common, they are very different on the most basic level. Apple introduces nsurlsession similar to the nsurlconnection concept, and you'll learn in this series of networking with Nsurlsession tutorial that nsurlsession is modern, easier to use, and and is attached to the original intention of mobile application design. What is nsurlsession?
Before I discuss the differences between Nsurlsession and nsurlconnection, it ' a good idea to ' a take ' What Nsurlsession is. Despite its name,nsurlsession isn ' t just another class can be in a IOS or OS X application. Nsurlsession is the foremost a technology just like nsurlconnection is.
Before discussing the difference between nsurlsession and nsurlconnection, you might want to take a closer look at what nsurlsession is. For Nsurlsession, it's just a class that can be used in IOS and OS x applications, like Nsurlconnection, as a technology for web development. Sessions are containers
Nsurlsession and Nsurlconnection both provide an APIs for interacting with various protocols, such as HTTP and HTTPS. The session object, a instance of Thensurlsession class, is what manages this interaction. It is a highly configurable container and a elegant API that allows for fine-grained control. It offers features that are absent in nsurlconnection. What ' s more, with nsurlsession, you can accomplish tasks that are simply don't possible with nsurlconnection, such as Implem Enting Private browsing.
Both Nsurlsession and Nsurlconnection provide APIs that interact with various protocols, such as HTTP and HTTPS. The session object, the Nsurlsession class object, is used to manage this interaction process. It is a highly configurable container that allows fine-grained management control by using the Appi provided. It provides all the features in Nsurlconnection, and it can also implement tasks that nsurlconnection cannot accomplish, such as private browsing. Tasks
The basic unit of work when working with Nsurlsession are the task, an instance ofnsurlsessiontask. There are three types of tasks, data tasks, upload tasks, anddownload tasks.
The most basic unit of using Nsurlsession is the task, which is an instance of Nsurlsessiontask. There are three kinds of tasks: Data Task,upload task and download task.
' ll most often use data tasks, which are instances Of nsurlsessiondatatask. Data tasks are used for requesting data from a server, such as JSON data. The principal difference with upload and download the tasks are this they return data directly to your application instead of G Oing through the file system. The data is only stored in memory. The most commonly used is the Data task, which is an instance of Nsurlsessiondatatask. The data task is used to request information from the server, such as JSON data. The main difference between it and the Upload task and Download task is that it returns data directly to the application rather than through the file system. This data is stored in memory only. As the name implies, upload tasks are used to upload data to a remote destination. The nsurlsessionuploadtask is a subclass Of nsurlsessiondatataskand behaves in a similar fashion. One of the key differences with a regular data task are that upload tasks can to used in a session created with a Backgroun D Session configuration. As the name suggests, the Upload task is used to upload data to a remote destination. Nsurlsessionuploadtask is a subclass of Nsurlsessiondatatask and behaves similarly. One of the main differences from the regular data task is that the Upload task can be used in background session management. Download tasks, inStances Of nsurlsessiondownloadtask, inherit directly fromnsurlsessiontask. The most significant difference with data tasks are that a download task writes it response directly to a temporary file. This is quite different from a regular data task, stores the response in memory. It is possible to cancel a download task And resume it in a later point. Download Task,nsurlsessiondownloadtask instance, directly inherits from Nsurlsessiontask. The most significant difference from the data task is that the Download task writes its response data to a temporary file, and the data task simply saves its response to memory. The Download task can be canceled or recovered at a later time.
As you can imagine, asynchronicity are a key concept in nsurlsession. Thensurlsession API returns data by invoking a completion handler or through the session ' s delegate. The API of Nsurlsession is designed with flexibility in mind as your ' ll notice a bit later in this tutorial.
As you can imagine, asynchrony is a key concept of nsurlsession. Using the Nsurlsession API to return data can be done by calling to complete the handler block or through a delegate to the session. The Nsurlsession API is designed with the flexibility in mind, and you'll see it later in this tutorial. Meet the Family
As I mentioned earlier, Nsurlsession is both a technology and a class so you'll be working with. The nsurlsession API houses a number of classes, but nsurlsession is the key component sending requests and receiving Onses. The configuration of the session object, however, are handled by a instance of the Nsurlsessionconfigurationclass. The Nsurlsessiontask class and its three concrete subclasses are the workers and are always-in-used with SES Sion as it is the session that creates the task objects.
As I mentioned earlier, nsurlsession refers to a new network development technology, but also a class. The Nsurlsession API includes several classes, but Nsurlsession is a critical part for sending requests and receiving responses, and the configuration of Session objects is handled by the Nsurlsessionconfiguration instance. Nsurlsessiontask and its three specific subclasses are always associated with session objects, because the conversation object creates the Task object. delegation
Both Nsurlsession and Nsurlconnection rely heavily on the delegation pattern. Thensurlsessiondelegate Protocol declares a handful of delegate methods for handling events at the Session-level. In addition, the Nsurlsessiontask class and subclasses all declare a delegate protocol for handling events.
Nsurlsession and nsurlconnection all adopt the principal-agent mode. The Nsurlsessiondelegate delegate protocol declares a small number of delegate methods to handle session events. In addition, Nsurlsessiontask and its subclasses each have a delegate protocol for handling task events. Old Friends
The nsurlsession API builds on top of classes so you ' re already familiar with, such asnsurl, Nsurlrequest, and Nsurlresp Onse.
The use of Nsurlsession APIs involves classes that you have been familiar with, such as Nsurl,nsurlrequest and Nsurlresponse. What are the differences?
How does nsurlsession differ from nsurlconnection? This is a important question, because Nsurlconnection is isn't being by Apple. Can still usensurlconnection in your projects. Why should you to use Nsurlsession?
So what's the difference between nsurlsession and nsurlconnection? This is a key issue because nsurlconnection has not been deprecated by Apple. You can still use nsurlconnection in a project. When to use the nsurlsession?
The "the" the "Understand is" the Nsurlsession instance is the object that manages the request and response. This is similar to how nsurlconnection works, but the key difference was that the configuration of the ' request is handled B ' Y the Session object, which is a long lived object. This are done through the nsurlsessionconfigurationclass. Not only does it provide the nsurlsession API fine-grained configuration through class, it E Ncourages the separation of data (request body) from metadata. The nsurlsessiondownloadtask illustrates this and directly writing the response to the file system.
The first thing to understand is that the Nsurlsession instance object is a management request and response, similar to nsurlconnection, but the difference is that the management request is configured with a long-lived object, completed by the Nsurlsessionconfiguration class object. The Nsurlsessionconfiguration class not only provides a fine-grained configuration API to manage Nsurlsession session objects, but also separates data (the request body) from metadata (metadata). Nsurlsessiondownloadtask is a good illustration of this by writing the response data directly to the file system.
Authentication is easier and handled more elegantly by nsurlsession. THENSURLSESSION API handles authentication on a connection basis instead the on a request basis, Like nsurlconnec Tion does. The nsurlsession api also makes it more convenient to provide HTTP options and the session can have a separate Storage container Depending on how to configure the session.