Safe Save behavior & Core Data

I have a Core Data application on Mac OS, using the SQLite option.

If I call [myMOC save:&anError];

All is well, no error.


But if I use [myDoc saveDocument:sender], I get the error--- "AppKit called rmdir("long path to temp folder"), it didn't return 0, and errno was set to 66"

And the save fails with a "Can't merge changes" error.

When I look in "long path to temp folder",the folder is empty.

Is there some strange timing issue inside the safe save black box? Does Core Data not play nice with safe save?

Replies

Answer based on Generic MacOS Document Based Project in the absense of sample code


Using a generic mac OS document type application there is no need to perform #1 below

1. save: is an instance method of NSManagedObjectContext


If you have a xcdatamodeld file in the project which happens to be empty this can cause runtime error when a save operation is attempted.

When I added an entity & attribute to the model saving no longer became an issue with app sandbox & file security set off.

Sorry I wasn't clear earlier. If I open an existing document, change one property of one object and call saveDocument(), I get the error message about failure to remove a non-empty folder in the TemporaryItems, as shown in my first message and "Could not merge changes" message. As an experiment I tried running the same test but calling [myMOC save, &error) and all was well.


The docs for NSPersistentDocument say that "For an existing document, a save just invokes

save:
on the context.", but I see different results.


I believe safe save saves the file to the TemporaryItems folder and then moves it to the desired URL. After the failed saveDocument call, the folder still exists, empty, but I get the error shown in my initial post. It's also possible that this error is not related to the merge failure, but they always appear together. I wonder if there's some strange timing issue with core data writing out the version to the temp folder while the safe save is trying to delete it.


The code that fails is:

[self.managedObjectContext processPendingChanges];

@try

{

[self saveDocument:self];

}

@catch (NSException *exception)

{

NSLog(@"Oops");

} @finally {

}

But I get no exception.


The code that doesn't fail, but which doesn't try a safe save is:

[self.managedObjectContext processPendingChanges];

[self.managedObjectContext save:&saveErr];

if (nil != saveErr)

{

NSLog(@"Failed to save Error: %@", saveErr);

}


Any pointers would be appreciated.