Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift downloading data from web

Tags:

swift

I'm watching udemy swift tutorial and I saw these two ways of getting data from web:

  1. Used for getting source code:
var url = NSURL(string: "SOME_URL");
var task = NSURLSession.sharedSession().dataTaskWithURL(url!,
    completionHandler: {
        (data, response, error) -> Void in
        // some code
    }
)
  1. Used for downloading an image:
let url = NSURL(string: "SOME_URL")        
let urlRequest = NSURLRequest(URL: url!)

NSURLConnection.sendAsynchronousRequest(urlRequest,
    queue: NSOperationQueue.mainQueue()) {
        (respone, data, error) -> Void in
        // some code
    }

My questions:

  • Which are differences between them?
  • Can I could download an image using first method and viceversa?
like image 394
Stevik Avatar asked Oct 31 '22 07:10

Stevik


1 Answers

NSURLSession -> This API provides a rich set of delegate methods for supporting authentication and gives your app the ability to perform background downloads when your app is not running or, in iOS, while your app is suspended.

The behavior of a session is determined by the configuration object used to create it. Because there are three types of configuration objects, there are similarly three types of sessions: default sessions that behave much like NSURLConnection, ephemeral sessions that do not cache anything to disk, and download sessions that store the results in a file and continue transferring data even when your app is suspended, exits, or crashes.

NSURLConnection -> The interface for NSURLConnection is sparse, providing only the controls to start and cancel asynchronous loads of a URL request. You perform most of your configuration on the URL request object itself.

You can use both types for downloading an image. For more reference please visit the link i specified.

like image 194
Alvin Varghese Avatar answered Nov 23 '22 02:11

Alvin Varghese