1

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!

Wizard of Kneup
  • 1,863
  • 1
  • 18
  • 35

2 Answers2

1

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.

Willeke
  • 14,578
  • 4
  • 19
  • 47
  • Thanks. I tried it. But removeAllActions undoes all my changes. However, I need my under-the-hood initiation as it is the meta-model for the users input. :-( – Wizard of Kneup Jun 29 '17 at 10:48
  • Actually, I forgot to take the first part into consideration. I now did both from within the document: `self.updateChangeCount(.changeCleared) db.managedObjectContext?.undoManager?.removeAllActions()` db is my top-level core data object. Together it works! – Wizard of Kneup Jun 29 '17 at 12:30
  • unfortunately, there was a subsequent error with my initiation not fully successful. I cannot recommend this approach. – Wizard of Kneup Jul 07 '17 at 13:49
  • I'm trying to figure out why it does work in my app. I'm adding the data in `initWithType:error:`. – Willeke Jul 07 '17 at 15:06
1

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
}
Dirk
  • 2,335
  • 24
  • 36