What is NSURLConnection class? Define its types and use cases

The NSURLConnection class may be used in two ways. The first is asynchronous, whereas the second is synchronous.

An asynchronous connection will start a new thread and complete the download operation there. While downloading the content and communicating, a synchronous connection will stop the calling thread.
Many programmers believe that a synchronous connection only affects the main thread, however this is not the case. A synchronous connection will always halt the thread from which it was initiated. The main thread will be stopped if we initiate a synchronous connection from it. When a synchronous connection is initiated from a thread other than the main thread, it behaves as an asynchronous connection and does not block the main thread.

In truth, the main difference between synchronous and asynchronous connections is that an asynchronous connection will generate a thread at runtime, whereas a synchronous connection would not.

To establish an asynchronous connection, we must first:

  1. Have our URL in an instance of NSString
  2. Convert our string to an instance of NSURL
  3. Place our URL in a URL Request of type NSURLRequest or, in the case of mutable URLs, in an instance of NSMutableURLRequest
  4. Create an instance of NSURLConnection and pass the URL request to it