So I'm having hard time understanding something. This are the things I understand about NSURSession :
Generally , I have 2 options for (as far as I know) DataTask(e.x dataTaskWithRequest) And DownloadTask(e.x DownloadTaskWithRequest) - Using their delegate method , or use the completion handler , Can't do both. I have managed to receive DATA using dataTaskWithRequest like this :
let request = NSMutableURLRequest(URL: dataSourceURL!) request.HTTPMethod = "POST" let postString = "lastid=\(id)" request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { println("error=\(error)") return } if data != nil { println("works") //Handle data } //println("response = \(response)") } task.resume()
It works perfectly. The problem is that I need to DOWNLOAD the data to the disk and not only to the memory(I'm downloading images). So I tried the same with DownloadTaskWithRequest + his completion handler and I have noticed that the parameters he takes are the same expect the first one which is NSURL and in DataTaskWithRequest is NSData so it makes things very simpler. ex.
let task2 = NSURLSession.sharedSession().downloadTaskWithRequest(request, completionHandler: { (location : NSURL!, response : NSURLResponse!, error : NSError?) -> Void in
if error != nil {
return
}
//How do I get the data??
})
task2.resume()
My Question is this : I know I can fetch the DATA out of the Location(NSURL) using :
var data = NSData(contentsOfURL: location)
1)Will contentsOfURL will make another "request" do get this data , or that he is working locally? If it sending request again , how can I avoid it?
2)Is this the right way(I know I can use the delegate methods, I prefer not)?
3)How can I store the data i have downloaded(after questions number 1 and 2 answered) locally , and access it if needed?
Thank you guys!! Sorry for newbie question , I really do care about efficient - Thank you!