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

3

7

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?

Xenogamy answered 9/6, 2012 at 2:37 Comment(0)
C
3

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.

Clancy answered 11/8, 2017 at 19:41 Comment(4)
But it won't autosave ever.Swirsky
@DaniSpringer Did you implement autosavesInPlace() with return true in your NSPersistentDocument class?Clancy
never heard of that. Any link to how to make one?Swirsky
developer.apple.com/documentation/appkit/nsdocument/…Clancy
O
3

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];
}
Odontograph answered 28/8, 2018 at 16:42 Comment(7)
How did you come up with that idea? It works for me, too. It even fixes the issue that occurs when you rename a document, edit it and save it afterwards (as a user, not by code). There is no manual saving involved. I am just afraid your solution could cause some side effects.Quotation
I know that the solution is a big hack, but it was the unique solution that I could find. I discovered this idea looking in all NSDocument class and lucky. My advice if that you don't save the NSManagedObjectContext manually in place you save document. But if you have background thread with other NSManagedObjectContext is complicated to manage these errors. I didnt find a good way for this. For other hand the errors caused by user's behaviours as renaming maybe it can be managed overriding some methods of NSDocument.Odontograph
I tested this in two of my apps and so far no problems. Thanks for the insight!Quotation
+ 50 if I could. It doesn't seem to have any side effects so far. This fixes all the "Domain=NSCocoaErrorDomain Code=67000 The document could not be saved. The file has been changed by another application." errors I have been getting.Comet
This only was a happy idea that it works :). I have this patch since 2 years and the app have presented any problemOdontograph
Works for me! If I can hazard a guess as to why - the only time I've seen this error in my app is on filesystems formatted with ExFAT, which only has 2-second precision for its file modification dates. The default NSDocument implementation might be making some assumptions about the document's attributes on disk that don't hold up outside of native Apple filesystems.Dearden
I remember that an easy way to reproduce it is by saving the context manually without using the document.Odontograph
B
-3

I think it has to do with the fact that you can navigate to other files by apple-clicking a method name. If you make changes in one file and then navigate to another it leaves the previous window behind. You can click the "back" triangle to reach it again (that is just above the upper left corner of the file window). But, if it had unsaved changes in that now-eclipsed window and you edit the same file again in a different tab or window XCode is going to ask you which one to keep with the above message.

When it happens, I copy the file on disk to a new name and then choose "Save Anyway" and compare the two files. Unfortunately, sometimes there are important changes in each file and I have to merge them by hand.

I'm so frustrated by this I could cry.

Brey answered 4/7, 2012 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.