简体   繁体   中英

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. 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. 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.

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! 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

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”? with 3 buttons Dont's save , cancel 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…

I found the solution to this issue!

In fact the application was waiting a reply to the NSTerminateLater. I needed to use the reply(toApplicationShouldTerminate:) function. So I just added NSApp.reply(toApplicationShouldTerminate: false) before returning from the guard statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM