0

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!

Micha Mazaheri
  • 3,481
  • 1
  • 21
  • 26
  • I assume you are using NSPersistentDocument, if so then new files get created in memory so the user has to save to a file name before closing the document or everything gets lost. Also there is a specific method that gets called which is specifically for you to set up initial data. It only gets called on new documents. I don't recall what it is but I can look it up if you need it. Exactly what issue are you wanting to address? – Duncan Groenewald Oct 28 '13 at 11:49
  • Thanks :) I actually couldn't find this magic method that allows to setup initial data. That's exactly what I was trying to do: make new documents come with some seed data. I think I found a way using the `NSUndoManager`. I'll post an answer on that. If you recall this method, please let me know! Thanks. Micha – Micha Mazaheri Oct 30 '13 at 09:54

1 Answers1

0

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.

Micha Mazaheri
  • 3,481
  • 1
  • 21
  • 26