0

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…

rmaddy
  • 314,917
  • 42
  • 532
  • 579
DEADBEEF
  • 1,930
  • 2
  • 17
  • 32
  • From the documentation of `save(to:ofType:for:delegate:didSave:contextInfo:)`: "When saving is completed, regardless of success or failure, the method sends the message selected by didSaveSelector to the delegate, with the contextInfo as the last argument.". Call the didSaveSelector if you don't call super. – Willeke Jan 09 '18 at 11:13
  • @Willeke Thanks for you comment. By the way I'm sorry but, how do we call a `didSaveSelector`? From what I found on the net I tried `Timer.scheduledTimer(timeInterval: 0.1, target: delegate, selector:didSaveSelector!, userInfo: contextInfo, repeats: false)` but I still have the same issue so I guess I do something wrong. – DEADBEEF Jan 09 '18 at 11:19
  • Did you try overriding `save(to:ofType:for:completionHandler:)` instead of `save(to:ofType:for:delegate:didSave:contextInfo:)`? – Willeke Jan 09 '18 at 11:34
  • @Willeke Yes, I just tried now with juste `guard let customURL = createCustomUrlFrom(url: url) else { return }` and `super.save(...)` and I've the same issue. What could disable quitting an app? – DEADBEEF Jan 09 '18 at 11:41
  • I had more information about my issue, I guess it may come from the windows prompted when we quit a app with unsaved edits. – DEADBEEF Jan 09 '18 at 11:52
  • Did you read the documentation of the save methods? You can't just return. – Willeke Jan 09 '18 at 13:06
  • @Willeke Yes I understood and that's my main issue ahah. Since I cannot just return and the selector call doesn't work, I've to say that I don't have any other ideas... – DEADBEEF Jan 09 '18 at 13:25

1 Answers1

1

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.

DEADBEEF
  • 1,930
  • 2
  • 17
  • 32