macos - 当启用 NSDocument 异步保存时,在简单的 -dataOfType :error:? 中调用 -unblockUserInteraction 是否有任何意义

标签 macos cocoa osx-lion

我有一个简单的 Mac OS X、Cocoa、基于文档的应用程序,它使用新的 10.7 自动保存、版本和异步保存 API。我完全使用NSDocument API 可免费获取所有 Apple 基于文档的应用程序功能。

为了支持新的 Lion Autosave/Versions/AsyncSaving,我在我的 NSDocument 中重写了以下方法像这样的子类:

@implementation MyDocument 
...
+ (BOOL)autosavesInPlace { return YES; }

- (BOOL)canAsynchronouslyWriteToURL:(NSURL *)URL ofType:(NSString *)type forSaveOperation:(NSSaveOperationType)op {
    return YES;
}

我还覆盖了 -dataOfType:error:帮助实现将文档数据保存到磁盘:

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outErr {
    NSData *data = nil;

    if ([typeName isEqualToString:MY_SUPPORTED_TYPE_NAME]) {
        data = makeSnapshotCopyOfMyDocumentData(); // assume return value is autoreleased
    } else if (outErr) {
        *outErr = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:nil];
    }

    // not sure this is doing much good, since i take no action after this.
    [self unblockUserInteraction];

    return data;
}
...
@end

看看我怎么打电话 -unblockUserInteraction最后在那里?

在支持新的 10.7 AsyncSaving 功能时,Apple 建议调用 -unblockUserInteraction尽早(在制作文档数据的快照副本之后)在 -dataOfType:error: 中执行。但苹果的例子表明,他们在调用-unblockUserInteraction后做了更多的工作。 .

但是,考虑到我在此之后没有采取任何其他行动,我想知道调用 -unblockUserInteraction 是否有任何意义?根本就在那里。

所以我的问题:

  1. 考虑到我在此之后没有采取任何其他行动,我调用 -unblockUserInteraction有什么好处吗?

  2. Apple 框架只需调用 -unblockUserInteraction紧接着 -dataOfType:error:无论如何都会返回?我应该把它留给他们吗?

最佳答案

我刚刚注意到 NSDocument documentation 之间存在细微的措辞差异以及 NSDocument.h 中的注释:

文档:

If saveToURL:ofType:forSaveOperation:completionHandler: is writing on a non-main thread because canAsynchronouslyWriteToURL:ofType:forSaveOperation: has returned YES, but it is still blocking the main thread, this method unblocks the main thread. Otherwise, it does nothing.

标题:

If -saveToURL:ofType:forSaveOperation:completionHandler: is writing on a non-main thread because -canAsynchronouslyWriteToURL:ofType:forSaveOperation: has returned YES, but is still blocking the main thread, unblock the main thread. Otherwise, do nothing.

我认为标题是最新的。

我正在开发一个应用程序,该应用程序在必须在主线程上运行的最后一行之后调用unblockUserInteraction。 (至少我是这么理解的)
我认为我们的代码符合 Apple 在设计 NSDocument 的异步保存部分时所考虑的场景:

fileWrapperOfType:中我们...

  • 为我们的文件包装器创建一个 QL 预览(必须在 主线程)...
  • 解锁用户交互 ...
  • ...“长时间”运行的文件保存任务(涉及压缩)

关于macos - 当启用 NSDocument 异步保存时,在简单的 -dataOfType :error:? 中调用 -unblockUserInteraction 是否有任何意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7936041/

相关文章:

pdf - Preview 在 OS X Lion 上的哪里存储 PDF 注释?

macos - 如何在Mail.app中运行的applescript中使用钥匙串(keychain)中的密码?

c - 为什么 C 程序打印 0D 而不是 0? (当 EOF 作为 Ctrl+D 发送时)

objective-c - 如何获取对 xib 文件中创建的 NSWindow 的引用?

cocoa - NSTextField输入键检测或firstResponder检测

macos - mac os x 中的 tar 命令添加 "hidden"文件,为什么?

mysql - 无法使用我的 mySQL root 密码登录 phpMyAdmin(在 Mac OS X Lion 上)

c - 如何检查 Linux/FreeBSD 上 write() 调用被阻止的问题?

macos - 为什么访问D类成员在OSX上崩溃?

objective-c - 使用 ParseKit 标记化和 DelimitedString