mercredi 30 avril 2014

Incremental saving of a UIDocument to improve write times


Vote count:

0




I am using a UIDocument to save a "file" which is really a collection of many files (images, audio, etc.).


In other words I'm using [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrappers] in my UIDocument subclass implementation of contentsForType.


The problem is that this takes a LONG time when there are many large files. There is no good reason I should be saving all of the files every time, though. Many of these files are images, etc. ("assets") which act as supporting files for my document, and are not changed frequently. Thus, my goal is to find some way to incrementally save the document: to only write the assets which have been changed since the document was opened.


Here is my current implementation of UIDocument.contentsForType:



- (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
NSMutableDictionary * wrappers = [NSMutableDictionary dictionary];

NSDictionary *metadataDict = [self.metadata packDetails]?:@{};
NSData *metadata = [NSJSONSerialization dataWithJSONObject:metadataDict options:NSJSONWritingPrettyPrinted error:nil];
NSFileWrapper *metadataWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:metadata];
[wrappers setObject:metadataWrapper forKey:NOTE_METADATA_FILENAME];

for(NSString *assetFilename in self.metadata.assets) {
NSData *data = [self dataForAsset:assetFilename];
if(data.length) {
NSFileWrapper * wrapper = [[NSFileWrapper alloc] initRegularFileWithContents:data];
[wrappers setObject:wrapper forKey:assetFilename];
}
}

NSFileWrapper * fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrappers];

return fileWrapper;

}


asked 33 secs ago






Aucun commentaire:

Enregistrer un commentaire