Alamofire MultiPartForm Files in NSTemporaryDirectory

I could not find the answer to my question anywhere, so I decided that I asked.

I use Alamofire 3.1.5 to download a fairly large volume of images, we are talking in hundreds of MB.

There is a code snippet:

  self.manager.upload(.POST, url, headers: headers, multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(fileURL: generalURL, name: "general", fileName: "general", mimeType: "image/jpeg") multipartFormData.appendBodyPart(fileURL: img1URL, name: "img1", fileName: "img1", mimeType: "image/jpeg") multipartFormData.appendBodyPart(fileURL: img2URL, name: "img2", fileName: "img2", mimeType: "image/jpeg") multipartFormData.appendBodyPart(fileURL: img3URL, name: "img3", fileName: "img3", mimeType: "image/jpeg") }, encodingCompletion: { encodingResult in . . . 

As I understand it, Alamofire processes this request, saving them to disk, for better optimization of RAM. This is reasonable, and I am very happy about it. It just works flawlessly.

On the other hand, this means that basically doubles the payload of data on the disk.

enter image description here

The fact is that these a files are not deleted, and even calls up the default iOS screen, warning that the device is working in free space.

I know how to delete the contents of this directory, but in my current code stream it’s safe to delete the contents after the completion of the entire request, it can even be 100 requests, and each of them takes about 20 MB of payload, so the fact is that the device can even not be able to store this amount of data.

My question is:

Can I force Alamofire to delete each of these files after it has successfully downloaded?

Sorry for the rather long question, I would post you potatoes, but this is not 9gag.

+5
source share
1 answer

In accordance with this question , you will need to delete it yourself.

It's simple, just delete all Alamofire files generated after receiving a response from the server. Here is how I did it:

 // Just some upload Alamofire.upload( .POST, uploadURL, multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(fileURL: somePath, name: "file") }, encodingCompletion: { encodingResult in switch encodingResult { case .Success(let upload, _, _): upload.responseJSON { response in if let JSON = response.result.value { /*** delete temp files Alamofire generated ***/ let temporaryDirectoryPath = NSTemporaryDirectory() let dir = NSURL(fileURLWithPath: temporaryDirectoryPath, isDirectory: true) .URLByAppendingPathComponent("com.alamofire.manager") .URLByAppendingPathComponent("multipart.form.data") do { try NSFileManager.defaultManager().removeItemAtPath(dir.path!) } catch {} } } } ) 
+2
source

Source: https://habr.com/ru/post/1241283/


All Articles