macos – 当启用NSDocument Asynchronous Saving时,在一个简单的-dataOfType中调用-unblockUserInteraction是否有任何意义:error:?

前端之家收集整理的这篇文章主要介绍了macos – 当启用NSDocument Asynchronous Saving时,在一个简单的-dataOfType中调用-unblockUserInteraction是否有任何意义:error:?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@
我有一个简单的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建议在-dataOfType:error:implementation中尽早调用-unblockUserInteraction(在对文档数据进行快照复制之后).但Apple的例子显示他们在调用-unblockUserInteraction后做了更多的工作.

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

所以我的问题:

>考虑到我之后没有采取任何其他行动,我对-unblockUserInteraction的调用是否正常?
> Apple框架是否在-dataOfType之后立即调用-unblockUserInteraction: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.

我认为Header是更新的.

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

在fileWrapperOfType中:我们……

>为我们的文件包装器创建一个QL预览(必须在主线)…> unblockUserInteraction …> …“长”运行文件保存任务(涉及压缩)

猜你在找的cocoa相关文章