I'm downloading a large file and when it finishes the
URLSession(session: NSURLSession,
downloadTask: NSURLSessionDownloadTask,
didFinishDownloadingToURL location: NSURL)
method gets called.
I want to save the resulting file locally, NSSearchPathDirectory.DocumentDirectory
for instance.
But I'm having some issues with this, mainly because the files are quite large 500MB+
I have tried:
NSFileManager.defaultManager().createFileAtPath(savePath,
contents: fileData,
attributes: nil)
Which works fine for smaller files < 100MB but larger files die with
Code=12 "Cannot allocate memory"
When trying to convert the downloaded file to NSData like this:
let data: NSData = try NSData(contentsOfURL: location,
options: NSDataReadingOptions.DataReadingMappedIfSafe)
The question is, is there another way to convert the location: NSURL
to NSData
just so there are no memory issues with large files?
Or is there a better way to save the large file other than NSFileManager.defaultManager().createFileAtPath()
just so it doesn't require NSData
?
Try using NSFileManager
's copyItemAtURL:toURL:error: to copy the file from location to wherever you want