core-datanspersistentdocument

NSPersistentDocument to be marked as not "dirty"/edited after internal initiation


I have a NSPersistentDocument (CoreData) that I initiate before I present it to the user. That means that I create some internal core data objects and add them to the document/persistent store/managed object context.

However, that means that even if no user activity is happening the document shows the saving dialog when the document is closed. I would like it to be marked as not dirty and no saving dialog as no real change happened.

Any idea? Many thanks in advance!


Solution

  • I handled that problem by implementing this in awakeFromNib:

    - (void)awakeFromNib {
        // Disable Undo
        [self.managedObjectContext processPendingChanges];
        [[self undoManager] disableUndoRegistration];
    
        // Do your initialization thing
    
        // Process changes to the object graph and reenable Undo
        [self.managedObjectContext processPendingChanges];
        [[self undoManager] enableUndoRegistration];
    
        // Rest of awakeFromNib, if any
    }