title: Core Data undoManager crashes
body: <p>I am using the below code for a core data singleton. Below is my code. (based from NachoMan's <a href="http://nachbaur.com/blog/smarter-core-data" rel="nofollow">blog</a>. However the code is from his gist.</p>
  
  <pre><code>// DataManager.h
  #import &lt;Foundation/Foundation.h&gt;
  #import &lt;CoreData/CoreData.h&gt;
  
  extern NSString * const DataManagerDidSaveNotification;
  extern NSString * const DataManagerDidSaveFailedNotification;
  
  @interface DataManager : NSObject {
  }
  
  @property (nonatomic, readonly, retain) NSManagedObjectModel *objectModel;
  @property (nonatomic, readonly, retain) NSManagedObjectContext *mainObjectContext;
  @property (nonatomic, readonly, retain) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  @property (nonatomic, readonly, retain) NSManagedObjectContext *managedObjectContext;
  
  + (DataManager*)sharedInstance;
  - (BOOL)save;
  - (BOOL)clearEntity:(NSString *)entityDescription;
  - (NSManagedObjectContext*)managedObjectContext;
  
  @end
  
  // DataManager.m
  #import "DataManager.h"
  
  NSString * const DataManagerDidSaveNotification = @"DataManagerDidSaveNotification";
  NSString * const DataManagerDidSaveFailedNotification = @"DataManagerDidSaveFailedNotification";
  
  @interface DataManager ()
  
  - (NSString*)sharedDocumentsPath;
  
  @end
  
  @implementation DataManager
  
  @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
  @synthesize mainObjectContext = _mainObjectContext;
  @synthesize objectModel = _objectModel;
  @synthesize managedObjectContext = _managedObjectContext;
  
  NSString * const kDataManagerBundleName = nil;//@"AP";
  NSString * const kDataManagerModelName = @"APData";
  NSString * const kDataManagerSQLiteName = @"APData.sqlite";
  
  + (DataManager*)sharedInstance {
      static dispatch_once_t pred;
      static DataManager *sharedInstance = nil;
  
      dispatch_once(&amp;pred, ^{ sharedInstance = [[self alloc] init]; });
      return sharedInstance;
  }
  
  - (void)dealloc {
      [self save];
  }
  
  - (NSManagedObjectModel*)objectModel {
      if (_objectModel)
          return _objectModel;
  
      NSBundle *bundle = [NSBundle mainBundle];
      if (kDataManagerBundleName) {
          NSString *bundlePath = [[NSBundle mainBundle] pathForResource:kDataManagerBundleName ofType:@"bundle"];
          bundle = [NSBundle bundleWithPath:bundlePath];
      }
      NSString *modelPath = [bundle pathForResource:kDataManagerModelName ofType:@"momd"];
      NSLog(@"Path: %@",modelPath);
      _objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
  
      return _objectModel;
  }
  
  - (NSPersistentStoreCoordinator*)persistentStoreCoordinator {
      if (_persistentStoreCoordinator) {
          NSLog(@"PersistentStore Exists %@",_persistentStoreCoordinator);
          return _persistentStoreCoordinator;
      }
      NSLog(@"Persistent Stored DOESN'T EXIST");
  
      // Get the paths to the SQLite file
      NSString *storePath = [[self sharedDocumentsPath] stringByAppendingPathComponent:kDataManagerSQLiteName];
      NSURL *storeURL = [NSURL fileURLWithPath:storePath];
  
      // Define the Core Data version migration options
      NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                   [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                   [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                   nil];
  
      // Attempt to load the persistent store
      NSError *error = nil;
      _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.objectModel];
      if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                 configuration:nil
                                   URL:storeURL
                                   options:options
                                     error:&amp;error]) {
          NSLog(@"Fatal error while creating persistent store: %@", error);
          abort();
      }
      NSLog(@"store: %@",_persistentStoreCoordinator);
      return _persistentStoreCoordinator;
  }
  
  - (NSManagedObjectContext*)mainObjectContext {
      if (_mainObjectContext)
          return _mainObjectContext;
  
      // Create the main context only on the main thread
      if (![NSThread isMainThread]) {
          [self performSelectorOnMainThread:@selector(mainObjectContext)
                         withObject:nil
                      waitUntilDone:YES];
          return _mainObjectContext;
      }
  
      _mainObjectContext = [[NSManagedObjectContext alloc] init];
      [_mainObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
  
      return _mainObjectContext;
  }
  
  - (BOOL)save {
      if (![self.mainObjectContext hasChanges])
          return YES;
  
      NSError *error = nil;
      if (![self.mainObjectContext save:&amp;error]) {
          NSLog(@"Error while saving: %@\n%@", [error localizedDescription], [error userInfo]);
          [[NSNotificationCenter defaultCenter] postNotificationName:DataManagerDidSaveFailedNotification
                                      object:error];
          return NO;
      }
  
      [[NSNotificationCenter defaultCenter] postNotificationName:DataManagerDidSaveNotification object:nil];
      return YES;
  }
  
  - (BOOL)clearEntity:(NSString *)entityDescription
  {
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
      NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:_managedObjectContext];
      [fetchRequest setEntity:entity];
  
      NSError *error;
      NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:&amp;error]; 
  
      for (NSManagedObject *managedObject in items) {
          [_managedObjectContext deleteObject:managedObject];
          NSLog(@"%@ object deleted",entityDescription);
      }
      if (![_managedObjectContext save:&amp;error]) {
          NSLog(@"Error deleting %@ - error:%@",entityDescription,error);
  
          return NO;
      }
      return YES;
  }
  
  - (NSString*)sharedDocumentsPath {
      static NSString *SharedDocumentsPath = nil;
      if (SharedDocumentsPath)
          return SharedDocumentsPath;
  
      // Compose a path to the &lt;Library&gt;/Database directory
      NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
      SharedDocumentsPath = [libraryPath stringByAppendingPathComponent:@"Database"];
  
      // Ensure the database directory exists
      NSFileManager *manager = [NSFileManager defaultManager];
      BOOL isDirectory;
      if (![manager fileExistsAtPath:SharedDocumentsPath isDirectory:&amp;isDirectory] || !isDirectory) {
          NSError *error = nil;
          NSDictionary *attr = [NSDictionary dictionaryWithObject:NSFileProtectionComplete
                                   forKey:NSFileProtectionKey];
          [manager createDirectoryAtPath:SharedDocumentsPath
             withIntermediateDirectories:YES
                      attributes:attr
                       error:&amp;error];
          if (error)
              NSLog(@"Error creating directory path: %@", [error localizedDescription]);
      }
  
      return SharedDocumentsPath;
  }
  
  - (NSManagedObjectContext*)managedObjectContext {
      if (_managedObjectContext) {
          return _managedObjectContext;
      }
      _managedObjectContext = [[NSManagedObjectContext alloc] init];
      NSUndoManager *undoManager = [[NSUndoManager alloc] init];
      [_managedObjectContext setUndoManager:undoManager];
      [_managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
      [_mainObjectContext setRetainsRegisteredObjects:YES];
  
      return _managedObjectContext;
  }
  
  @end
  </code></pre>
  
  <p>However, in the <code>managedObjectContext</code> if I add</p>
  
  <pre><code>NSUndoManager *undoManager = [[NSUndoManager alloc] init];
  [_managedObjectContext setUndoManager:undoManager];
  </code></pre>
  
  <p>it will crash. </p>
  
  <p><em>Crittercism</em> logs show</p>
  
  <p>SIGBUS<br>
  main (main.m:16)</p>
  
  <pre><code>0    CoreData 0x0033d940 -[NSManagedObject(_NSInternalMethods) _newSnapshotForUndo__] + 352
  1    CoreData 0x00318fb1 -[NSManagedObjectContext(_NSInternalChangeProcessing) _registerUndoForOperation:withObjects:withExtraArguments:] + 193
  2    CoreData 0x0031922f -[NSManagedObjectContext(_NSInternalChangeProcessing) _registerUndoForInsertedObjects:] + 63
  3    CoreData 0x003148f8 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 1384
  4    CoreData 0x00314389 -[NSManagedObjectContext processPendingChanges] + 41
  5    CoreData 0x002e8bd8 _performRunLoopAction + 216
  6    CoreFoundation 0x0177d99e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
  7    CoreFoundation 0x01714640 __CFRunLoopDoObservers + 384
  8    CoreFoundation 0x016e04c6 __CFRunLoopRun + 1174
  9    CoreFoundation 0x016dfd84 CFRunLoopRunSpecific + 212
  10   CoreFoundation 0x016dfc9b CFRunLoopRunInMode + 123
  11   GraphicsServices 0x023617d8 GSEventRunModal + 190
  12   GraphicsServices 0x0236188a GSEventRun + 103
  13   UIKit 0x007e2626 UIApplicationMain + 1163
  14   My-App 0x284d main (main.m:16)
  15   My-App 0x27b5 start + 53
  </code></pre>
  
  <p>Why is it crashing with these 2 lines when alot of SO posts say to use it? If I take these 2 lines out it won't crash, I am just not able to use undo manager.</p>