Use Your Loaf

[[brain engage] write]

NSFileManager defaultManager Is Not Thread Safe

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.

Comments