Search code examples
iosnsfilemanager

FileManager.fileExistsAtPath prior to FileManager.removeItem?


in iOS is it good practice to make sure a file exists before it is deleted, or is it fine to just go ahead and try to delete a file that may or may not exist and let the error fail silently in a catch block?

I'm iterating over a large list of local files to delete of which about 10% probably will not exist.


Solution

  • From the apple documentation for the fileExistsAtPath: method:

    Attempting to predicate behavior based on the current state of the file system or a particular file on the file system is not recommended. Doing so can cause odd behavior or race conditions. It’s far better to attempt an operation (such as loading a file or creating a directory), check for errors, and handle those errors gracefully than it is to try to figure out ahead of time whether the operation will succeed.