0
votes

I have problems saving my MOC in a PersistentDocument.

I insert a new MO in my MOC and do a save (for e.g.)

    NSManagedObject *person=[[NSManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext] insertIntoManagedObjectContext:self.managedObjectContext];

    if(person)
    { 
        [person setValue:@"test" forKey:@"name"];
        NSError *error;
        [self.managedObjectContext save:&error];
    } 

I must save the MO at this point because I need a NOT temporary objectID of it for my background thread to do some calculation on the entity.

But when I save the MOC in this way, I get a requester

The document “Untitled.binary” could not be saved. The file has been changed by another application.

Click Save Anyway to keep your changes and save the changes made by the other application as a version, or click Revert to keep the changes from the other application and save your changes as a version.

the next time the Document is trying to save its content. I can not save the document instead, because it is possible that it is still an untitled document and a call of [document save] would open the save requester which ist not very comfortable for the user.

Is there any solution?

Thanks

Claus

1
Is it possible you have two copies of your program running? How are you setting up the persistent store? - paulmelnikow
No this is a standard document based app template. You can reproduce this very simple. Just create a new Xcode project document based app and insert a MO programmatically and after that save the MOC. Now you cant save the document from the menu without getting this error - Claus Bönnhoff

1 Answers

0
votes

Instead of trying to save you MOC, try

[self.managedObjectContext processPendingChanges];

I had the same issue on an App I'm working on where changes just made would not be there to reference. I tried saving the MOC and keep getting the same message. Calling the processPendingChanges worked for me.