title: iOS 4 Application Termination and Undo
body: <p>What is a good way to handle iOS 4 app termination when using an <code>NSUndoManager</code>?</p>
  
  <p>I have an iPhone (iOS 4) application that uses Core Data. The app allows the user to edit managed objects, and I implement undo using <code>NSUndoManager</code> in a straightforward manner: Before displaying an editor view modally, I create a new <code>NSUndoManager</code> for the managed object context. I also begin undo grouping so that any changes can easily be undone if the user taps the "Cancel" button. If the user taps "Save," I simply remove the undo manager and the changes become permanent. So far, so good.</p>
  
  <p>If the user presses the Home button (or takes a call) in the midst of editing an object, the app gets suspended. It sends the app delegate an <code>applicationDidEnterBackground</code> message and I use that opportunity to save the managed object context. The context, of course, contains the new edits, just waiting to be undone by the <code>NSUndoManager</code>.</p>
  
  <p>Here's the issue: If the app is later "unsuspended," the <code>NSUndoManager</code> still exists and everything works fine. However, if the app gets "killed in its sleep" by the OS, the undo stack is lost and the changes made to the object now become permanent. At relaunch I want to restore the app to the exact same state it was in before it was suspended, but that seems to require me to save and restore the undo stack. Unfortunately, I couldn't find an obvious way to do that. </p>
  
  <p>Is there a good way to support undo so that it works consistently whether or not an app is terminated? I hope I'm missing something obvious. Any help or suggestions would be appreciated.</p>