2

I am using Alamofire for download data from the server. I wanted to save in directory whichever is the safest place(DocumentDirectory, NSTemporaryDirectory). Could anyone help me in this?

DocumentDirectory:

   let destination: DownloadRequest.DownloadFileDestination = { _, _ in
        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let fileURL = documentsURL.appendingPathComponent(fileName)

        return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }

NSTemporaryDirectory

    let fileURL = URL(fileURLWithPath:  
    NSTemporaryDirectory()).appendingPathComponent(fileName)
    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
            return (fileURL, [.createIntermediateDirectories, 
.removePreviousFile])
        }
Vandana
  • 49
  • 3
  • 7

1 Answers1

5

NSTemporaryDirectory will be wipe out data once your iOS device is running out of memory. So you can store temporary data on NSTemporaryDirectory like caching images and other stuff.

NSDocumentDirectory is useful to store data for long time.

So as per your need you need to use these directories to store data.

For more info please check is saving in NSDocumentDirectory okay?

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48