我开发了一个基于文档的Cocoa应用程序,允许异步保存文档。也就是说,我的NSDocument子类返回canAsynchronouslyWrite(to:typeOf:for:)
上的ture。
如果文档内容正在编辑,我希望动态且无声地延迟(或取消)常规自动保存。起初,当我在checkAutosavingSafety()
中抛出错误时,我认为它已经足够了,但它会为用户显示错误消息对话框。
我相信这种标准需求有一种标准方式。但我不确定在NSDocument子类中我应该阻止保存以及我应该说哪种方法"请等待"。
有人对此有任何想法吗?
作为参考,文档的内容是由NSTextView子类管理的文本。
答案 0 :(得分:0)
我终于发现在使用.userCalcelled
的保存过程中抛出autosavingIsImplicitlyCancellable
错误可以取消自动保存。
/// make autosaving cancellable
override var autosavingIsImplicitlyCancellable: Bool {
return true
}
/// save or autosave the document contents
override func save(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, completionHandler: @escaping (Error?) -> Void) {
// cancel if something is working
guard saveOperation != .autosaveInPlaceOperation || !self.isEditing else {
completionHandler(CocoaError(.userCancelled))
return
}
super.save(to: newUrl, ofType: typeName, for: saveOperation, completionHandler: completionHandler)
}
/// whether your document is currently being edited
var isEditing: Bool {
// check your document state
}