Why does AppKit destroy my Saved Application State folder during normal application termination?

SETTINGS

  • OS X 10.9.0 (13A603)
  • Xcode Version 5.0.2 (5A3005)
  • Normal Cocoa Document Application

I developed a sample application based on OS X documents.

I intend to support Apple's UI save feature to restore open documents / Windows from a previous application launch.

This is supported by default in document-based applications .

I am not overriding any of the related methods, such as:

-[NSWindow isRestorable] -[NSResponder encodeRestorableStateWithCoder:] -[NSResponder decodeRestorableStateWithCoder:] -[NSApplication restoreWindowWithIdentifier:state:completionHandler:] -[NSDocumentController restoreWindowWithIdentifier:state:completionHandler:] 

(Note: -[NSWindow isRestorable] already returns YES in my document windows by default. Presumably, this is a document-based application function)

I do not think that I am doing something unusual in my application related to saving the user interface.

Problem:

Saving the user interface in my application does not work. But this is not very strange. . When I launch my application (creating and running it in Xcode or launching the assembly independently), the normal Saved Application State folder is created correctly and the information about the application state is filled in with the correct information.

Correct Saved Application State Folder Contents

If I force-quit my application (suddenly stopping the running Xcode build or through the Dock Force Quit menu item) and then restart the application, the save user interface function works fine. The application finds the information stored in the Saved Application State folder and correctly restores open documents / Windows from a previous launch.

However , when I usually close my application , the entire Saved Application State folder is deleted (I see this happening in Finder). So the next time I run the application, there will be no saved state for recovery, and the recovery will fail.

I tracked the deletion of this folder before it was ever called -[NSApplication terminate:] .

So I put the Symbolic breakpoint on unlink() , and, of course, the one-on-one code in AppKit intentionally deletes the Saved Application State folder and all its contents.

enter image description here

Notice the unlink() call from the queue named NSPersistentUI I/O in the main thread from NSApplicationMain . (This is clearly not from my code)

Why does AppKit destroy my Saved Application State folder during normal application termination?

This is normal?

How can i stop this?

+6
source share
2 answers

Make sure this field is not checked under System Preferences> General:

Close windows when quitting an application

When this check box is selected, it disables the state recovery system and leads to the behavior you described.

+9
source

We override the @indragie flag for our sandbox application, as this field is not checked for new accounts, and therefore the critical state recovery function is lost.

The code is as simple as adding this to your main () procedure:

 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSQuitAlwaysKeepsWindows"]; // override stupid global preference, so our state restore works properly 
+12
source

All Articles