Sunday, June 16, 2013

NSFileWrapper NSFileWrapperWritingAtomic example in Objective C (iOS).

NSFileWrapper NSFileWrapperWritingAtomic

File Wrapper Writing Options
Writing options that can be set by the writeToURL:options:originalContentsURL:error: method.

enum {
NSFileWrapperWritingAtomic = 1 << 0,
NSFileWrapperWritingWithNameUpdating = 1 << 1
typedef NSUInteger NSFileWrapperWritingOptions;

Whether writing is done atomically.
You can use this option to ensure that, when overwriting a file package, the overwriting either completely succeeds or completely fails, with no possibility of leaving the file package in an inconsistent state. Because this option causes additional I/O, you shouldn't use it unnecessarily. For example, don't use this option in an override of -[NSDocument writeToURL:ofType:error:], because NSDocument safe-saving is already done atomically.
Whether descendant file wrappers are sent the setFilename: method if the writing succeeds.
This option is necessary when your application passes a URL in the originalContentsURL parameter to the writeToURL:options:originalContentsURL:error: method. Without using this option (and reusing child file wrappers properly), subsequent invocations of writeToURL:options:originalContentsURL:error: would not be able to reliably create hard links in a new file package, because the record of names in the old file package would be out of date.
NSFileWrapper NSFileWrapperWritingAtomic example.
- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
    NSFileWrapper *wrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
    [wrapper addRegularFileWithContents:[@"please work" dataUsingEncoding:NSUTF8StringEncoding] preferredFilename:@"foobar"];
    [wrapper writeToURL:absoluteURL options:NSFileWrapperWritingAtomic originalContentsURL:nil error:outError];

    NSDictionary *metadata = [NSDictionary dictionaryWithObject:@"0.1" forKey:@"Version"];
    NSURL *mdURL = [NSURL fileURLWithPath:[[absoluteURL path] stringByAppendingPathComponent:@"SiteInfo.plist"]];
    [metadata writeToURL:mdURL atomically:YES];

    return YES;

Example of [NSFileWrapper NSFileWrapperWritingAtomic].
NSFileManager *fileManager = [NSFileManager defaultManager];
NSFileWrapper *fw = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
NSArray *subpaths = [fileManager subpathsAtPath:picsPath];
for (NSString *path in subpaths) {
   NSString *longPath = [picsPath stringByAppendingPathComponent:path];
   NSString *filename = [NSString stringWithFormat:@\"%@.jpg\", [[path componentsSeparatedByString:@\".\"] objectAtIndex:0]];
   NSData *pngData = [fileManager contentsAtPath:longPath];
   UIImage *pngImage = [UIImage imageWithData:pngData];
   NSData *jpgData = UIImageJPEGRepresentation(pngImage, 1.0f);
   [fw addRegularFileWithContents:jpgData preferredFilename:filename];
NSURL *destURL = [NSURL URLWithString:[NSString stringWithFormat:@\"file://%@\",[exportPath stringByAppendingPathComponent:@'Export']]];
[fw writeToURL:destURL options:NSFileWrapperWritingAtomic originalContentsURL:nil error:nil];

End of NSFileWrapper NSFileWrapperWritingAtomic example article.