Avoid uploading the file again if it hasn't changed
I need to load an XML file using HTTP protocol to use it locally in my iPhone application. Sometimes this file will be updated on the server, but not very often.
How do you compare an already uploaded file that is already in the Documents folder with the one on the server by simply uploading it if the content has been updated on the server?
source to share
When downloading the file for the first time, keep the date from the Last-Modified header of the response. You can pull it out of the object NSHTTPURLResponse
.
In subsequent runs, place this date in the header If-Modified-Since
NSURLRequest
. If the file on the server has not changed, the statusCode
in NSHTTPURLResponse
should be 304 (which means "Not changed"), and the response body should be empty.
source to share
If you want to move the document directory to find a specially named file, use NSFileManager -fileExistsAtPath:isDirectory
and compare it with the name of the uploaded file ( -suggestedFilename
as long as you use NSURLResponse) with -isEqualToString
.
If you need to get the revision date, use NSFileManager -attributesOfItemAtPath:error:
in combination with the NSFileModificationDate key.
source to share