Re: Core Data completely unable to find the source object model for migration
Re: Core Data completely unable to find the source object model for migration
- Subject: Re: Core Data completely unable to find the source object model for migration
- From: Matteo Manferdini <email@hidden>
- Date: Thu, 6 Aug 2009 11:30:56 +0200
> Did you create a mapping model?
> If not, did you specify the NSInferMappingModelAutomaticallyOption option?
Yes, I did create one. But I think that the problem is not there,
since not knowing the source model, Core Data cannot infer the correct
mapping model.
For the manual migration I'm sure of this, because the code for the
mapping model is below the one for the source model (that returns
nil). This is my code (for manual migration)
NSString *applicationSupportFolder = [self applicationSupportFolder];
NSError *storeMetadataError = nil;
NSString *persistentStorePath = [applicationSupportFolder
stringByAppendingPathComponent: @"Heicos.sql"];
NSURL *persistentStoreURL = [NSURL fileURLWithPath:persistentStorePath];
NSDictionary *persistentStoreMetadata = [NSPersistentStoreCoordinator
metadataForPersistentStoreOfType:NSSQLiteStoreType
URL:persistentStoreURL error:&storeMetadataError];
if (!persistentStoreMetadata)
[NSApp presentError:storeMetadataError];
//! If the model isn't compatible with the store, migrates the store
NSManagedObjectModel *destinationModel = [self managedObjectModel]; //
See note below
if (![destinationModel isConfiguration:nil
compatibleWithStoreMetadata:persistentStoreMetadata])
{
NSManagedObjectModel *sourceModel = [NSManagedObjectModel
mergedModelFromBundles:nil forStoreMetadata:persistentStoreMetadata];
// Returns nil
NSString *sourceStorePath = [persistentStorePath
stringByAppendingString:@"~"];
[[NSFileManager defaultManager] movePath:persistentStorePath
toPath:sourceStorePath handler:nil];
NSMigrationManager *migrationManager = [[NSMigrationManager alloc]
initWithSourceModel:sourceModel destinationModel:destinationModel];
NSURL *sourceStoreUrl = [NSURL URLWithString:sourceStorePath];
NSMappingModel *mappingModel = [NSMappingModel
mappingModelFromBundles:nil forSourceModel:sourceModel
destinationModel:destinationModel];
NSError *migrationManagerError;
if (![migrationManager migrateStoreFromURL:sourceStoreUrl
type:NSSQLiteStoreType options:nil withMappingModel:mappingModel
toDestinationURL:persistentStoreURL destinationType:NSSQLiteStoreType
destinationOptions:nil error:&migrationManagerError])
[NSApp presentError:migrationManagerError];
}
Note:
I tried to implement the managedObjectModel: method in three ways, but
none of them works:
1) XCode Core Data template code:
NSMutableSet *allBundles = [[NSMutableSet alloc] init];
[allBundles addObject: [NSBundle mainBundle]];
[allBundles addObjectsFromArray: [NSBundle allFrameworks]];
managedObjectModel = [NSManagedObjectModel
mergedModelFromBundles:[allBundles allObjects]];
return managedObjectModel;
2) Keeping the models in the .xcdatamodeld directory
managedObjectModel = [[NSManagedObjectModel alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"MacZone_DataModel" ofType:@"momd"]]];
return managedObjectModel;
3) Moving the models out of the .xcdatamodeld directory, making them
top level resources in the main bundle:
managedObjectModel = [[NSManagedObjectModel alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"MacZone_DataModel" ofType:@"mom"]]];
return managedObjectModel;
Thank you.
--
Matteo Manferdini
Pawn Software
www.pawn-soft.com
_______________________________________________
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