Core Data: *Really* Automatic Migration : Span Multiple Versions
Core Data: *Really* Automatic Migration : Span Multiple Versions
- Subject: Core Data: *Really* Automatic Migration : Span Multiple Versions
- From: Jerry Krinock <email@hidden>
- Date: Wed, 5 May 2010 12:55:13 -0700
Say that a Core Data document-based app package contains:
model version 1
mapping model 1->2
model version 2
mapping model 2->3
model version 3
It seems to me that a document created when version 1 was current will not be openable by the latest version of the app, at least with the 10.5 SDK, because the "automatic" migration performed by -[NSPersistentDocument
configurePersistentStoreCoordinatorForURL:::::] is not smart enough to search for and apply mapping models serially.
One solution to this problem is to edit mapping model 1->2 into a new combined mapping model 1->3. Not much fun when you get up to version 6 and need to edit and test five mapping models.
A better solution would be to override -[NSPersistentDocument
configurePersistentStoreCoordinatorForURL:::::] to do the required searching and serial migrations. Disadvantage is that it might take a really long time to open a really old document, but that's acceptable.
Something like this:
Get store metadata for the document to be opened.
Read the "VersionInfo" plist with NSPropertyListSerialization method (yuck).
Extract from plist the current model version.
Extract from plist an array of all model versions.
Get managed object model of current model version and send it -isConfiguration:compatibleWithStoreMetadata:.
If it returns YES, goto Finish:.
For each other model version, get managed object model and send it -isConfiguration:compatibleWithStoreMetadata:, until you find one that is compatible with the document.
Call this the sourceModel.
Label TryMigrate:
Send +[NSMappingModel mappingModelFromBundles:forSourceModel:destinationModel:] with different destination models until you get a mapping model. Call the successful destination the destinModel.
Create a NSMigrationManager with sourceModel, destinModel.
Send this migration manager the message -migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error], creating a temporary file.
If migration succeeded, rename the source file, adding a tilde, and rename the temporary file to have the name of the original document.
If destinModel is current model version, goto Finish:.
Set sourceModel to destinModel.
Go back to TryMigrate:
Label Finish:
Invoke super -configurePersistentStoreCoordinatorForURL:::::.
Has anyone already done this?
Any suggestions before I try it?
_______________________________________________
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