1
votes

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!

2

2 Answers

1
votes

The dirty state is connected to documentEdited. documentEdited is set by updateChangeCount:. updateChangeCount: is automatically called by the undo manager.

Call [[self undoManager] removeAllActions]; or its Swift equivalent to remove the dirty state.

1
votes

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
}