|
| 1 | +// |
| 2 | +// Copyright (c) 2013 Luke Scott |
| 3 | +// https://github.com/lukescott/LSFileWrapper |
| 4 | +// Distributed under MIT license |
| 5 | +// |
| 6 | + |
| 7 | +#import "LSDocument.h" |
| 8 | +#import "LSFileWrapper.h" |
| 9 | + |
| 10 | +@interface LSFileWrapper () |
| 11 | +@property (nonatomic, strong) NSURL *url; |
| 12 | +@end |
| 13 | + |
| 14 | +@implementation LSDocument |
| 15 | + |
| 16 | +- (BOOL)writeContents:(LSFileWrapper *)contents |
| 17 | + andAttributes:(NSDictionary *)additionalFileAttributes |
| 18 | + safelyToURL:(NSURL *)url |
| 19 | + forSaveOperation:(UIDocumentSaveOperation)saveOperation |
| 20 | + error:(NSError *__autoreleasing *)outError |
| 21 | +{ |
| 22 | + return [contents writeUpdatesToURL:self.fileURL error:outError]; |
| 23 | +} |
| 24 | + |
| 25 | +- (void)saveWithCompletionHandler:(void (^)(BOOL success))completionHandler |
| 26 | +{ |
| 27 | + [self saveToURL:self.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:completionHandler]; |
| 28 | +} |
| 29 | + |
| 30 | +- (BOOL)writePathFirst:(LSFileWrapper *)file |
| 31 | + andAttributes:(NSDictionary *)additionalFileAttributes |
| 32 | + safelyToURL:(NSURL *)url |
| 33 | + forSaveOperation:(UIDocumentSaveOperation)saveOperation |
| 34 | + error:(NSError *__autoreleasing *)outError |
| 35 | +{ |
| 36 | + return [file writeUpdatesToURL:url error:outError]; |
| 37 | +} |
| 38 | + |
| 39 | +- (void)savePathFirst:(NSString *)path |
| 40 | + firstHandler:(void (^)(BOOL success))firstHandler |
| 41 | + completionHandler:(void (^)(BOOL success))completionHandler |
| 42 | +{ |
| 43 | + NSError *contentError; |
| 44 | + NSURL *url = [self.fileURL copy]; |
| 45 | + LSFileWrapper *contents = [self contentsForType:self.fileType error:&contentError]; |
| 46 | + if(!contents) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + LSFileWrapper *file = [contents fileWrapperWithPath:path]; |
| 51 | + NSURL *firstURL = [url URLByAppendingPathComponent:path]; |
| 52 | + |
| 53 | + [self performAsynchronousFileAccessUsingBlock:^{ |
| 54 | + NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self]; |
| 55 | + |
| 56 | + [fileCoordinator coordinateWritingItemAtURL:url |
| 57 | + options:NSFileCoordinatorWritingForMerging |
| 58 | + error:nil |
| 59 | + byAccessor: |
| 60 | + ^(NSURL *newURL) { |
| 61 | + NSError *writeError; |
| 62 | + BOOL wroteFirst, wroteAll; |
| 63 | + |
| 64 | + wroteFirst = [self writePathFirst:file |
| 65 | + andAttributes:nil |
| 66 | + safelyToURL:firstURL |
| 67 | + forSaveOperation:UIDocumentSaveForOverwriting |
| 68 | + error:&writeError]; |
| 69 | + |
| 70 | + if(firstHandler) { |
| 71 | + dispatch_async(dispatch_get_main_queue(), ^(void) { |
| 72 | + firstHandler(wroteFirst); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + if(wroteFirst) { |
| 77 | + wroteAll = [self writeContents:contents |
| 78 | + andAttributes:nil |
| 79 | + safelyToURL:url |
| 80 | + forSaveOperation:UIDocumentSaveForOverwriting |
| 81 | + error:&writeError]; |
| 82 | + } |
| 83 | + |
| 84 | + if(completionHandler) { |
| 85 | + dispatch_async(dispatch_get_main_queue(), ^(void) { |
| 86 | + completionHandler(wroteAll); |
| 87 | + }); |
| 88 | + } |
| 89 | + }]; |
| 90 | + }]; |
| 91 | +} |
| 92 | + |
| 93 | +- (BOOL)readFromURL:(NSURL *)url error:(NSError *__autoreleasing *)outError |
| 94 | +{ |
| 95 | + __block LSFileWrapper *wrapper = [[LSFileWrapper alloc] initWithURL:url isDirectory:NO]; |
| 96 | + __block BOOL result; |
| 97 | + dispatch_sync(dispatch_get_main_queue(), ^(void) { |
| 98 | + result = [self loadFromContents:wrapper |
| 99 | + ofType:self.fileType |
| 100 | + error:outError]; |
| 101 | + }); |
| 102 | + [wrapper loadCache]; |
| 103 | + return result; |
| 104 | +} |
| 105 | + |
| 106 | +@end |
0 commit comments