Hi all,
I’ve never used NSFileCoordinator class, simply because I haven’t had any need for it until now. And now that I’m about to need it rather soon, I’m trying to get a grasp on it, but it does’t go very well so far. Even though the documentation reads pretty clear, I don’t seem to get it correctly. Also, I haven’t found any example online, which would be of any value to me, so I’m taking my chances here. In order to begin to understand it, I’ll start with the simplest real case scenario, for which I don’t even understand yet whether NSFileCoordinator is the way to go.
Let’s say, I have a bunch of directories in a parent directory (so, all are at the same level in the file system hierarchy), which I want to compress and pack in a ZIP archive (for example) programatically (e.g. using libarchive). After selecting which directories to zip, I’d like to ensure that once the zipping operation has started, any changes to all files and directories in the hierarchies of selected directories to be packed will stay unchanged, until the zipping operation is complete. That means, no file anywhere in the hierarchies should be modified in any way and no directory or file anywhere in the hierarchies should be added, deleted or moved.
I thought I would achieve this by using one NSFileCoordinator and a bunch NSFileAccessIntent objects, each for every directory at the top level of the hierarchies. The intents will be for reading without changing, because that’s what I want to do; just to read those directories and files (and zip them) without making any changes to them. The code would be something like this (I want to do the operation asynchronously):
NSMutableArray *intents = [NSMutableArray arrayWithCapacity:[sources count]];
for (NSURL *source in sources)
[intents addObject:[NSFileAccessIntent readingIntentWithURL:source options:NSFileCoordinatorReadingWithoutChanges]];
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[coordinator coordinateAccessWithIntents:intents queue:[NSOperationQueue new] byAccessor:^(NSError *error)
{
// Do the zipping here.
}];
It looks simple and straightforward, but it doesn’t really work as I thought it would. Once the operation is started, I can modify files and directories the way I described above, from within the same application/process, as well as others (using NSFileCoordinator approach), like Finder.
Hence, I’d really appreciate if someone can point me in the right direction. For example, what needs to be done to achieve this simple example I described above. Once I get some pointers, I may start understanding those classes and documentation in the right way and work myself through more complex examples and use cases.
Thanks for any insights.
-- Dragan