iosnsurlsessiontempnsurlsessionuploadtask

Will temporary files be saved by an NSURLSessionUploadTask


I am implementing a resumable upload protocol that uploads in the background on iOS, which means I have to use an NSURLSessionUploadTask with a file. Since it's a resumable upload protocol, the file needs to be truncated based on the data that has already been received by the server, so I need to save a new temporary file to disk that has only the bytes to be uploaded within it.

If I can create that temporary upload file in the tmp/ or /Library/Caches/, can I trust that it will be kept as long as the NSURLSession is running?

EDIT: When an upload fails, the server will be saving the bytes it has already received and communicating that to the client. The client then should only send part of the file, which is why I need to create a smaller temporary file that must not be deleted mid-upload.


Solution

  • Huh? You provide the entire file, and the system takes care of managing the partial upload/download, and notifies you once the transfer is complete. In the case of a download, t hands you a temporary file once the download is complete and you have to save it to a permanent location.

    You should not be mucking around with partial files at all.

    EDIT:

    You don't have access to tmp or /Library/Caches/, except through the sandbox. You can get access to the caches directory with the call

    [NSSearchPathForDirectoriesInDomains(
      NSCachesDirectory, 
      NSUserDomainMask, YES) lastObject];
    

    It's my understanding that the caches directory only gets purged on restart, or if the device gets critically low on space, but I seem to remember that the docs are vague on when, exactly, the caches directory gets cleared.

    You would probably be better off saving your file to the documents directory, then deleting it once you're done with it.