Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle large files with NSData?

I have a very large video and I need to chunk this video to upload it to Dropbox.

I tried to use NSData, but because this file is too large, my application always crashes, so I don't know what I can do now.

For smaller videos, I used this:

NSData(contentsOfURL: self.newAsset.URL)!.subdataWithRange(NSMakeRange(0, 10000000))

and I didn't have any problem with that, but when the video is too large I have an error:

Cannot allocate memory

So, what can I do to chunk the data of large videos?

like image 359
testa abalez Avatar asked Oct 29 '22 22:10

testa abalez


1 Answers

For best practice go with NSURLSession if you want to implement custom otherwise lots for third party library are there like RESTKit or AFNetworking. For NSURLSession the session NSURLSession supports three types of tasks: data tasks, download tasks, and upload tasks. All it support the background uploads/downloads as well. source(apple developer)

  • Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests from your app to a server. Data tasks can return data to your app one piece at a time after each piece of data is received, or all at once through a completion handler.
  • Download tasks retrieve data in the form of a file, and support background downloads while the app is not running.
  • Upload tasks send data in the form of a file, and support background uploads while the app is not running.

Image Source raywenderlich.com Image Source:

like image 146
Buntylm Avatar answered Nov 15 '22 05:11

Buntylm