NSFileManager defaultManager is not thread safe

Update 07 April 2013: The threading considerations when using the shared NSFileManager instance changed with the introduction of iOS 5:

The methods of the shared NSFileManager object can be called from multiple threads safely. However, if you use a delegate to receive notifications about the status of move, copy, remove, and link operations, you should create a unique instance of the file manager object, assign your delegate to that object, and use that file manager to initiate your operations.

The example code in the post for using either the shared or unique instance remain valid but the advice now is that you are safe to use the shared instance unless you need to use the NSFileManagerDelegate protocol methods.

Original Post 12 June 2011

The NSFileManager class provides a convenient way to perform a number of common file-system operations within an iOS application. The class is very versatile including methods to create, copy, move and delete files and directories. To show just one example, consider the following code fragment to test if a file exists and copy it to a new destination:

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:sourceFile]) {

  NSError *error = nil;
  if (![fileManager copyItemAtPath:sourceFile
                            toPath:destFile
                             error:&error]) {
    // Deal with error
  }
}

Nothing too remarkable about this and I am sure this is a fairly common usage of NSFileManager. The defaultManager class method returns the default NSFileManager object for the file system - in other words it provides a singleton object you can use to perform file system operations. However there is one small caveat that users of NSFileManager should be aware of. If you take a look at the Class documentation for NSFileManager the overview section contains this warning:

In iOS and Mac OS X v 10.5 and later you should consider using [[NSFileManager alloc] init] rather than the singleton method defaultManager. Instances ofNSFileManager are considered thread-safe when created using [[NSFileManager alloc] init].

This means that if your application ends up using the NSFileManager singleton from more than one thread you may get some unexpected results or even end up with corrupted files in your file system. The thread safe version of the above code fragment would look like this:

NSFileManager *fileManager = [[NSFileManager alloc] init];

if ([fileManager fileExistsAtPath:sourceFile]) {

  NSError *error = nil;
  if (![fileManager copyItemAtPath:sourceFile
                            toPath:destFile
                             error:&error]) {
    // Deal with error
  }
}

[fileManager release];

In other words the thread safe version is hardly any more complicated than the version using the non thread safe singleton. An instance of NSFileManager is allocated, initialised and then used as before. You need to remember to release it when your done but otherwise it is no big deal. So given that you never know how code you write today might be used tomorrow my feeling is that you are better off just always using the thread-safe version and avoiding the defaultManager class method all together.

Never miss a post!

iOS Size Classes Cheat Sheet

Subscribe and get my free iOS Size Classes Cheat Sheet

Success! Now check your email to confirm your subscription and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
No time to watch WWDC videos?

Sign up to get my iOS posts direct to your inbox and I will send you a free PDF of my iOS Size Classes Cheat Sheet.

OK! Check your inbox (or spam folder) for an email to confirm your details and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
Archives Categories