简体   繁体   English

如果取消NSDocument中的保存操作,则无法退出应用

[英]Cannot quit the app if I cancel the save operation in a NSDocument

I'm subclassing the NSDocument class in order have a specific behavior during the save. 我将NSDocument类子类化,以便在保存期间具有特定的行为。 I save the project in a folder and I create specific sub-folders with audio files used for this project. 我将项目保存在文件夹中,并使用用于该项目的音频文件创建特定的子文件夹。

I override the following function save(to:ofType:for:delegate:didSave:contextInfo) but I noticed a weird behaviour. 我重写了以下函数save(to:ofType:for:delegate:didSave:contextInfo)但是我注意到了一个奇怪的行为。 Let's say I've the following implementation: 假设我有以下实现:

override func save(to url: URL,
                   ofType typeName: String,
                   for saveOperation: NSDocument.SaveOperationType,
                   delegate: Any?,
                   didSave didSaveSelector: Selector?,
                   contextInfo: UnsafeMutableRawPointer?) {

    guard let customURL = createCustomUrlFrom(url: url) else { return }

    super.save(to: customURL,
               ofType: typeName,
               for: saveOperation,
               delegate: delegate,
               didSave: didSaveSelector,
               contextInfo: contextInfo)
}

I try to create a custom URL and if I don't manage to do it I cancel the save operation. 我尝试创建一个自定义URL,如果我不设法这样做,则取消保存操作。

Now if I quit the app before saving, the app prompts me to save. 现在,如果我在保存之前退出应用程序,则该应用程序会提示我保存。 If I cannot create the custom URL and I return (before the super.save… call), the quit button or "cmd+q" doesn't work! 如果无法创建自定义URL并return (在super.save…调用之前),则退出按钮或“ cmd + q”不起作用! I've to force quit to close the app. 我不得不退出以关闭该应用程序。

Anyone see what I did wrong here? 有人看到我在这里做错了吗? Does something is running in the background that prevents me to close the app? 后台是否正在运行某些阻止我关闭该应用程序的内容?

UPDATE UPDATE

Maybe it comes from the sheet prompted when we quit an edited document. 可能来自我们退出已编辑文档时提示的工作表。 We have the window saying Do you want to save the changes made to the document “Untitled”? 我们在窗口中说Do you want to save the changes made to the document “Untitled”? with 3 buttons Dont's save , cancel save... 使用3个按钮Dont's savecancel save...

If I click on save, then the project already exists I show a window to let the user replace the project or cancel the save (it's done in the createTargetUrlFrom(url:) func. If the user choose to cancel the app cannot be quit. So I think about the first window running in background maybe… 如果单击保存,则该项目已经存在,我会显示一个窗口,让用户替换该项目或取消保存(在createTargetUrlFrom(url:)函数中完成。如果用户选择取消该应用程序,则无法退出。所以我想也许第一个窗口在后台运行...

I found the solution to this issue! 我找到了解决此问题的方法!

In fact the application was waiting a reply to the NSTerminateLater. 实际上,该应用程序正在等待对NSTerminateLater的答复。 I needed to use the reply(toApplicationShouldTerminate:) function. 我需要使用reply(toApplicationShouldTerminate :)函数。 So I just added NSApp.reply(toApplicationShouldTerminate: false) before returning from the guard statement. 所以我只是在从保护声明返回之前添加了NSApp.reply(toApplicationShouldTerminate: false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CloudKit - 如何取消正在进行的保存/上传操作 - CloudKit -How to Cancel a Save/Upload Operation that's in Progress 如何在NSDocument macOS应用程序中以编程方式打开文档? - How to open programmatically a document in a NSDocument macOS app? 打开基于NSDocument的应用程序会加载两个窗口 - Opening NSDocument based app loads two windows NSDocument:如何保存和恢复文档的窗口位置? - NSDocument: how do you save & restore a document's window position? xcode swift:NSUserDefaults:如果退出应用程序,则保存高分,并在中断的地方重新启动 - xcode swift: NSUserDefaults : save the highscore if quit app and restart it where left off CoreData:这个 NSPersistentStoreCoordinator 没有持久存储。 它无法执行保存操作 - CoreData: This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation operation.cancel()不取消当前操作 - operation.cancel() not cancelling current operation 取消通过操作启动的 PHImageRequest - Cancel a PHImageRequest launched via an Operation 从情节提要中启动的初始应用无法调用NSDocument初始化 - Initial app launch from storyboard fails to call NSDocument init 防止基于 NSDocument 的应用程序在崩溃后重新打开文档 - Prevent NSDocument-based app from reopening documents after a crash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM