我有一个简单的 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
是否有任何意义?根本就在那里。
所以我的问题:
考虑到我在此之后没有采取任何其他行动,我调用
-unblockUserInteraction
有什么好处吗?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/