Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire MultiPartForm files in NSTemporaryDirectory

I have not been able to find answer to my question anywhere so I figured I've ask.

I am using Alamofire 3.1.5 for uploading rather large volume of pictures, 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 Alamofire handles creating those request by saving them to disk, for better RAM optimalization. Which is smart and I am really happy about it. It just work flawless.

On the other hand that means that it is basically doubling the data payload on disk.

enter image description here

The things is that those files a are not getting deleted, It even causes iOS default screen warning that the device is running low on free space.

I know how to delete content of this directory, but in my current code flow it is safe to delete the content after all the request are finished, it may be even 100 requests, and each one of them takes roughly 20MB of payload. So the thing is that the device might not even have the capacity of storing this amount of data.

My question is:

Can I make Alamofire to delete every single one of these files after it gets successfully uploaded?

Sorry for rather long question, I would post you a potato here, but this is not 9gag.

like image 743
Tomas Sykora Avatar asked Jan 23 '16 00:01

Tomas Sykora


1 Answers

According to this this issue, you will need to delete it yourself.

It's simple, just delete all files Alamofire generated after you get a response from server. Here's 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 {}

        }
      }
    }
  )
like image 188
yuji Avatar answered Nov 17 '22 05:11

yuji