Core Data: Doc Migrates, Opens, but wants to migrate back to ~ on Save!
Core Data: Doc Migrates, Opens, but wants to migrate back to ~ on Save!
- Subject: Core Data: Doc Migrates, Opens, but wants to migrate back to ~ on Save!
- From: Jerry Krinock <email@hidden>
- Date: Tue, 24 Nov 2009 20:16:17 -0800
To make a long story short, let's start over...
In a Core Data document-based app, I open an old document which must be migrated to the current version. Leopard Automatic migration apparently succeeds. The old file is renamed with a tilde, and the document opens. However, when I edit and Save, first a warning sheet is displayed telling me that it is going to rename the file -- with a tilde. Arghh! That's the old file now! Predictably, upon "OK", it fails, with sqlite reporting "no such column" for an attribute that was added in the new version.
After the warning sheet is dismissed, during my override of saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:, sending -fileURL gives the name with the tilde. So I tried to fool it in there by sending back setFileURL: with the tilde removed before invoking super. Didn't work. Same error.
Likewise if I swap the document filenames in Finder while the warning sheet is displayed. Can't fool Core Data.
What might be causing my document to try and migrate back to the old version during Save?
Thanks,
Jerry
SOME ADDITIONAL INFO:
In a previous thread, I posted an override of initWithContentsOfURL:ofType:error: which I used to set the NSMigratePersistentStoresAutomaticallyOption:
http://lists.apple.com/archives/Cocoa-dev/2009/Nov/msg01133.html
My app is currently using that code. I found an even simpler idea from mmalc, here:
http://homepage.mac.com/mmalc/CocoaExamples/MigratingDepartmentAndEmployees.zip
He overrides configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error: instead. However I found that this override, with or without my override, makes things worse. An old document will not even open and not even give a meaningful error, failing immediately with NSCocoaErrorDomain code 256, "The document "xxxxx.bkmslf" could not be opened." Weird -- mmalc's code makes perfect sense to me. Here is mmalc's code that I am NOT using:
- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url
ofType:(NSString *)fileType
modelConfiguration:(NSString *)configuration
storeOptions:(NSDictionary *)storeOptions
error:(NSError **)error {
BOOL ok;
NSMutableDictionary *newStoreOptions;
if (storeOptions == nil) {
newStoreOptions = [NSMutableDictionary dictionary];
}
else {
newStoreOptions = [storeOptions mutableCopy];
}
[newStoreOptions setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
ok = [super configurePersistentStoreCoordinatorForURL:url
ofType:fileType
modelConfiguration:configuration
storeOptions:newStoreOptions
error:error];
// Comment by Jerry: NSLog of 'ok' gives '1' here.
if (ok)
{
// If all went well, update the metadata
NSPersistentStoreCoordinator *psc = [[self managedObjectContext] persistentStoreCoordinator];
NSPersistentStore *pStore = [psc persistentStoreForURL:url];
// configurePersistentStoreCoordinatorForURL is called when document reopened
// Check for existing metadata to avoid overwriting unnecessarily
id existingMetadata = [[psc metadataForPersistentStore:pStore]
objectForKey:(NSString *)kMDItemKeywords];
if (existingMetadata == nil)
{
ok = [self setMetadataForStoreAtURL:url];
}
}
return ok;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden