App freezes / locks up when writing very large files

I'm downloading and writing ~200mb podcasts into the Documents directory with the following code:

var podcastRequest = NSURLRequest(URL: audioUrl)
NSURLConnection.sendAsynchronousRequest(podcastRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if let myPodcastDataFromUrl = NSData(contentsOfURL: audioUrl) {

    if myPodcastDataFromUrl.writeToURL(destinationUrl, atomically: true) {

        // add to the array to track the download
        var tempDic = self.posts[theRow] as! NSMutableDictionary as NSMutableDictionary
        tempDic["downloaded"] = "true"
        self.posts[theRow] = tempDic
    } else {
        println("Error saving file")


The sendAsynchronousRequest call prevents the lockup from happening during the download, but the app still freezes when it starts actually writing it to the directory.

Is there a way to prevent the lockup from happening at all, or am I going to have to write smaller chunks at a time?


You won't be able to store 200MB in memory before trying to write it to disk, but you can use downloadTaskWithURL method, it writes the file to a temporary folder and you can move it when it finishes to the documents folder as follow.

let documentsDirectoryURL =  NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as! NSURL

NSURLSession.sharedSession().downloadTaskWithURL(audioUrl, completionHandler: {
    (location, response, error) -> Void in

    if let error = error {
    else {
        if NSFileManager().moveItemAtURL(location, toURL: documentsDirectoryURL.URLByAppendingPathComponent(response.suggestedFilename!), error: nil) {
        } else {
            println("not saved.")

Need Your Help

How to convert PHP's crc32 hash into the MySQL equivalent?

php sql hash

Apparently MySQL's CRC32() function returns an unsigned BIGINT, while PHP returns hexadecimal value.

How does linker resolves duplicate symbols in dynamically loadable libraries?

c linux build linker dynamic-linking

I have two dynamically loadable libraries and and etc. Both have a global variable named protocol which is initialized to "SMTP" and "POP" respectively. I have another static