Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDocument: The document could not be autosaved. The file has been changed by another application

A search on the title of this post reveals that it's pretty common; indeed, I've gotten this error from Xcode. But I can't seem to find any fixes. I'm seeing it now when I run my program, and it appears to occur during or after changeCountTokenForSaveOperation is called. It seems related to the undo manager, rather than to the fact that i'm using core data, but I may be wrong.

Does anyone know what causes this or how to fix it?

like image 371
pickwick Avatar asked Jun 09 '12 02:06

pickwick


People also ask

Why can't I autosave my Documents?

Some users have only had the autosave issue occur when working with extremely large files; others, when they have lots of documents open. The General preference pane lets you set continuous saving for supported apps.

What happens to the current state of the document when working?

As you work away, the current state of the document is updated temporarily. When you close the file, changes are applied even if you don’t use the Save command or keystroke.

Why am I getting a nspersistentdocument error when saving a document?

This error can occur with NSPersistentDocument when you perform a manual save in code on the managedObjectContext of your NSPersistentDocument class. The problem here is you're modifying the document on disk behind NSPersistentDocument's back.

How to avoid the error message when saving a document?

So the correct solution is avoid saving manually. If you cannot avoid it, you can override fileModificationDate method of NSDocument to return the current file modification date of the file. Of this way the document does not show the error message. Show activity on this post.


2 Answers

This error can occur with NSPersistentDocument when you perform a manual save in code on the managedObjectContext of your NSPersistentDocument class. The problem here is you're modifying the document on disk behind NSPersistentDocument's back. Just leave the save actions to the NSPersistentDocument and the error will not occur.

like image 146
Ely Avatar answered Sep 28 '22 03:09

Ely


The problem is saving manually the managedObjectContext. So the correct solution is avoid saving manually. If you cannot avoid it, you can override fileModificationDate method of NSDocument to return the current file modification date of the file. Of this way the document does not show the error message.

- (NSDate *)fileModificationDate {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDictionary *attrs = [fileManager attributesOfItemAtPath:self.fileURL.path error:NULL];
    return attrs[NSFileModificationDate];
}
like image 44
93sauu Avatar answered Sep 28 '22 02:09

93sauu