Here's my issue:
I have a document-based application, all written using Core Data. When a new document is created (on app start, or using doing File > New, or Cmd+N), I need this document to be populated with initial seed objects. Let's say my document is a notebook, so I want new document to contain an empty note when created.
So I innocently insert my objects (-[NSEntityDescription initWithEntity: insertIntoManagedObjectContext:
), and it works.
My issue here, is that the document appears to be edited right after being opened. Which is a terrible UX. Moreover, when closing this document without adding any further changes, the user is prompted to save it (super bothering!).
So what I tried so far to fix this problem, is setting the actions as discardable on my undo-manager (-[NSUndoManager setActionIsDiscardable:]
to YES
on [myDocument undoManager]
). But it doesn't change anything.
To debug, I overrode -[NSDocument isDocumentEdited]
and always returned NO
. It fixes partially the issue. Of course, I don't want to deal with that stuff, I guess it should be fixed on the undo-manager/persistent document/managed object context side.
Anyone faced the same issue?
Thank you all very much!
I finally figured out a way to do using the undo manager's removeAllActions
:
- (id)initWithType:(NSString *)typeName error:(NSError *__autoreleasing *)outError`
{
// Add your seed data in Core Data
// Erase all actions to make the document appear as new
[[self undoManager] removeAllActions];
}
It fixes all my issues, allow the document to be closed without being prompted to save, not showing the Edited menu on the title and not allowing users to undo the seed insertion actions.