objective-c - NSFileWrapper,延迟加载和保存

标签 objective-c macos cocoa nsdocument nsfilewrapper

我有一个基于 NSDocument 的应用程序,它使用文件包装器来保存和加载其数据。 文档可以有各种资源,所以我不想把所有东西都加载到内存中。我可能在做一些根本性的错误,但是一旦我更改了一个(内部)文件然后保存,我就无法读取任何尚未加载到内存中的文件。

我已将相关代码分离到一个单独的项目中以重现此行为,我得到了相同的结果。基本流程是这样的:

  • 我从磁盘加载现有文档。主 fileWrapper 是一个包含两个其他文件包装器(sub1sub2)的目录文件包装器(我称之为 main)。此时加载两个内部文件包装器。
  • 当用户想要编辑sub1时,它是从磁盘加载的。
  • 用户保存文档
  • 如果用户想要编辑其他文件 (sub2),则无法加载。出现的错误:

    -[NSFileWrapper regularFileContents] tried to read the file wrapper's contents lazily but an error occurred: The file couldn’t be opened because it doesn’t exist.
    

下面是我项目中的相关代码:

这段代码在这个要点中可能更容易阅读: https://gist.github.com/bob-codingdutchmen/6869871

#define FileName01 @"testfile1.txt"
#define FileName02 @"testfile2.txt"

/**
 *  Only called when initializing a NEW document
 */
-(id)initWithType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
    self = [self init];
    if (self) {
        self.myWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];

        NSLog(@"Initializing new document...");

        NSString *testString1 = @"Lorem ipsum first sub file";
        NSString *testString2 = @"This is the second sub file with completely unrelated contents";

        NSFileWrapper *w1 = [[NSFileWrapper alloc] initRegularFileWithContents:[testString1 dataUsingEncoding:NSUTF8StringEncoding]];
        NSFileWrapper *w2 = [[NSFileWrapper alloc] initRegularFileWithContents:[testString2 dataUsingEncoding:NSUTF8StringEncoding]];

        w1.preferredFilename = FileName01;
        w2.preferredFilename = FileName02;

        [self.myWrapper addFileWrapper:w1];
        [self.myWrapper addFileWrapper:w2];

    }
    return self;
}

-(NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {

    // This obviously wouldn't happen here normally, but it illustrates
    // how the contents of the first file would be replaced
    NSFileWrapper *w1 = [self.myWrapper.fileWrappers objectForKey:FileName01];
    [self.myWrapper removeFileWrapper:w1];

    NSFileWrapper *new1 = [[NSFileWrapper alloc] initRegularFileWithContents:[@"New file contents" dataUsingEncoding:NSUTF8StringEncoding]];
    new1.preferredFilename = FileName01;

    [self.myWrapper addFileWrapper:new1];

    return self.myWrapper;
}

-(BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
    self.myWrapper = fileWrapper;
    return YES;
}

- (IBAction)button1Pressed:(id)sender {
    // Read from file1 and show result in field1
    NSFileWrapper *w1 = [[self.myWrapper fileWrappers] objectForKey:FileName01];
    NSString *string1 = [[NSString alloc] initWithData:w1.regularFileContents encoding:NSUTF8StringEncoding];
    [self.field1 setStringValue:string1];
}

- (IBAction)button2Pressed:(id)sender {
    // Read from file2 and show result in field2
    NSFileWrapper *w2 = [[self.myWrapper fileWrappers] objectForKey:FileName02];
    NSString *string2 = [[NSString alloc] initWithData:w2.regularFileContents encoding:NSUTF8StringEncoding];
    [self.field2 setStringValue:string2];
}

底部的两个方法仅用于更新 UI,因此我可以看到会发生什么。

  • 为了更改文件的内容,我删除了现有的 fileWrapper 并添加了一个新的。这是我发现更改文件内容的唯一方法,也是我在其他 SO 答案中看到的方法。

  • 当从磁盘加载文档时,我会保留 fileWrapper 以便我可以使用它(在上面的代码中称为 myWrapper)

Apple 文档说 NSFileWrapper 支持延迟加载和增量保存,所以我假设我的代码有一些我看不到的基本缺陷。

最佳答案

NSFileWrapper 本质上是一个 unix 文件节点的包装器。如果文件被移动,包装器仍然有效。

您似乎遇到的问题是在保存过程中创建一个新的文件包装器是一个新文件夹。并且系统会删除您之前的包装器,包括 sub2

要实现您想要的效果,您需要更改为增量保存,即仅将更改的部分保存到位。请参阅 NSDocument 中的“就地保存”。

关于objective-c - NSFileWrapper,延迟加载和保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19228884/

相关文章:

objective-c - 使用事件点击使用 OSX 鼠标/触控板事件

iphone - 项目文件放在哪里?

cocoa - 全屏隐藏 NSToolbar

macos - productsign mac .pkg 安装程序

objective-c - 收听我的文本字段的值更改

macos - 如何在 Cocoa 应用程序中显示文件列表?

swift - 使用仅在子类中可用的 Cocoa Bindings 绑定(bind)到属性

iphone - MKStoreKit -isSubscriptionActive 总是返回 False

iphone - Objective-c 中的代表

ios 以毫秒为单位向 UITableView 添加数据