Core Data - Migration question? - iphone

I am trying to do a migration
I have 2 versions of model
1.xcdatamodel
2.xcdatamodel
I created a mapping model from version 1 to 2
1to2.xcmappingmodel
The problem is that it can't find the migration model that I created so mappingModel always gets nil.
Is there anything I have to do to specify what mappingModel it ahould use?
target = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
//target and source are initialized correctly
mappingModel = [NSMappingModel mappingModelFromBundles:nil forSourceModel:source destinationModel:target];

It might be that you changed one of your models after creating the mapping model.
Even if a change does not seem relevant it will change the hash value of the model which is used for finding the appropriate mapping model.
At least I've been bitten by this just now :-)

If you've already created a mapping model from 1.xcdatamodel to 2.xcdatamodel, and properly configured it, then you should be able to do something like this: [Note: the key is specifying NSMigratePersistentStoresAutomaticallyOption]
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (persistentStoreCoordinator)
return persistentStoreCoordinator;
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"MyStore.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
error:&error])
{
// Handle error
NSLog(#"Error adding persistent store...%#", error);
// Handle the error.
NSLog(#"Failed to save to data store: %#", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0)
{
for(NSError* detailedError in detailedErrors)
{
NSLog(#" DetailedError: %#", [detailedError userInfo]);
}
}
else
{
NSLog(#" %#", [error userInfo]);
}
}
else
{
DLog(#"Persistent store added without incident, apparently.");
}
return persistentStoreCoordinator;
}

To answer the original question, your code looks alright but I'm not why you passed nil as the bundles parameter. The documentation doesn't say one can. So:
NSArray *theBundles = [NSArray arrayWithObject:[NSBundle mainBundle]];
mappingModel = [NSMappingModel mappingModelFromBundles:theBundles
forSourceModel:source
destinationModel:target];

If you pass nil as bundle parameter, it will take [NSBundle mainBundle].
[To response to question of Elise van Looij]

Related

Core data update model with app update?

everyone who work with Core Data know the message "the model used to open the store is incompatible with the one used to create the store".
Then I have to delete my app from simulator, and rebuilding it again.
My question is if I submit an app v 1.0, then add some entities to core data in v 1.1, does this mean that the users of 1.0 who updated to 1.1 will have their data cleared up?
You will need to create a new model version for your model, and migrate the database. You can do a lightweight migration if your model changes are within the required changes. If not, you will need to tell core data how to migrate your data. Check the migration documentation: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreDataVersioning/Articles/Introduction.html
In your case it sounds like a simple extension to your old data model. If you just really add some new entities or even new classes then the so called leightweight migration is the right way to go for you.
Actually in this case you almost do not have anything to do, but create your second model IN ADDITION to your original model. It is important, that you have BOTH model, then the app will just load your 1st version without any problems as well as the new version.
Don't forget to mark your new model as the new one!
Try to be careful when creating the new model, since deleting a model is a real hassle.
Your code will look very similar to this:
-(NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *lC = [self persistentStoreCoordinator];
if (lC != nil) {
managedObjectContext =[[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: lC];
}
return managedObjectContext;
}
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
// Allow inferred migration from the original version of the application.
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"DBName.sqlite"]];
NSError *error = nil;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl
options:options error:&error]){
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
}
return persistentStoreCoordinator;
}
- (NSManagedObjectModel *) managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return managedObjectModel;
}

CoreData versioning and blocking lightweight migration

I have enabled versioning of my Core Data model and have been using light weight migration. My code always tries to do lightweight migration and then if that fails because the model are incompatible it falls back to deleting all existing data and refetching from the server.
So lightweight migration is just used for efficiency and isn't required for correctness.
What I want to do now is make a change to my model which in theory lightweight migration could handle but in fact I need new data from the server. I want to somehow flag the model and not upgradeable via lightweight migration. For example if a field name has not changed but the meaning of that field has changed in such a way that the old code is incompatible with the new code base. (This is just an example.)
Has anyone found a way to flag a two models as incompatible so lightweight migration won't upgrade them?
I've struggled with the same problem before.
I have a method that will attempt to migrate data using Mapping Models which is what you should use if you're going to turn off lightweight migration.
If you aren't going to do a lot of fancy data mapping, xcode will automatically create a mapping model that will work exactly like lightweight migration. All you have to do is create a new "Mapping Model" file each time you add a new version to Core Data. Just go to "File -> New -> New File" and under Core Data there should be a Mapping Model template. Select it and choose the source and destination versions.
I don't have my code openly available on github so I'll just post the migration method here.
- (BOOL)progressivelyMigrateURL:(NSURL*)sourceStoreURL ofType:(NSString*)type toModel:(NSManagedObjectModel*)finalModel
{
NSError *error = nil;
// if store dosen't exist skip migration
NSString *documentDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
if(![NSBundle pathForResource:#"YongoPal" ofType:#"sqlite" inDirectory:documentDir])
{
migrationProgress = 1.0;
[self performSelectorOnMainThread:#selector(updateMigrationProgress) withObject:nil waitUntilDone:YES];
// remove migration view
[self.migrationView performSelectorOnMainThread:#selector(setHidden:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:YES];
[self.migrationView performSelectorOnMainThread:#selector(removeFromSuperview) withObject:nil waitUntilDone:YES];
self.migrationView = nil;
self.migrationProgressLabel = nil;
self.migrationProgressView = nil;
self.migrationSpinner = nil;
return YES;
}
//START:progressivelyMigrateURLHappyCheck
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:type URL:sourceStoreURL error:&error];
if (!sourceMetadata)
{
return NO;
}
if ([finalModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata])
{
migrationProgress = 1.0;
[self performSelectorOnMainThread:#selector(updateMigrationProgress) withObject:nil waitUntilDone:YES];
// remove migration view
[self.migrationView performSelectorOnMainThread:#selector(setHidden:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:YES];
[self.migrationView performSelectorOnMainThread:#selector(removeFromSuperview) withObject:nil waitUntilDone:YES];
self.migrationView = nil;
self.migrationProgressLabel = nil;
self.migrationProgressView = nil;
self.migrationSpinner = nil;
error = nil;
return YES;
}
else
{
migrationProgress = 0.0;
[self.migrationView performSelectorOnMainThread:#selector(setHidden:) withObject:NO waitUntilDone:YES];
[self performSelectorOnMainThread:#selector(updateMigrationProgress) withObject:nil waitUntilDone:YES];
}
//END:progressivelyMigrateURLHappyCheck
//START:progressivelyMigrateURLFindModels
//Find the source model
NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:nil forStoreMetadata:sourceMetadata];
if(sourceModel == nil)
{
NSLog(#"%#", [NSString stringWithFormat:#"Failed to find source model\n%#", [sourceMetadata description]]);
return NO;
}
//Find all of the mom and momd files in the Resources directory
NSMutableArray *modelPaths = [NSMutableArray array];
NSArray *momdArray = [[NSBundle mainBundle] pathsForResourcesOfType:#"momd" inDirectory:nil];
for (NSString *momdPath in momdArray)
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSString *resourceSubpath = [momdPath lastPathComponent];
NSArray *array = [[NSBundle mainBundle] pathsForResourcesOfType:#"mom" inDirectory:resourceSubpath];
[modelPaths addObjectsFromArray:array];
[pool drain];
}
NSArray* otherModels = [[NSBundle mainBundle] pathsForResourcesOfType:#"mom" inDirectory:nil];
[modelPaths addObjectsFromArray:otherModels];
if (!modelPaths || ![modelPaths count])
{
//Throw an error if there are no models
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:#"No models found in bundle" forKey:NSLocalizedDescriptionKey];
//Populate the error
error = [NSError errorWithDomain:#"com.yongopal.coredata" code:500 userInfo:dict];
if([[self.prefs valueForKey:#"debugMode"] isEqualToString:#"Y"])
{
NSLog(#"error: %#", error);
}
return NO;
}
//END:progressivelyMigrateURLFindModels
//See if we can find a matching destination model
//START:progressivelyMigrateURLFindMap
NSMappingModel *mappingModel = nil;
NSManagedObjectModel *targetModel = nil;
NSString *modelPath = nil;
for(modelPath in modelPaths)
{
targetModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
mappingModel = [NSMappingModel mappingModelFromBundles:nil forSourceModel:sourceModel destinationModel:targetModel];
//If we found a mapping model then proceed
if(mappingModel)
{
break;
}
else
{
//Release the target model and keep looking
[targetModel release];
targetModel = nil;
}
}
//We have tested every model, if nil here we failed
if (!mappingModel)
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:#"No mapping models found in bundle" forKey:NSLocalizedDescriptionKey];
error = [NSError errorWithDomain:#"com.yongopal.coredata" code:500 userInfo:dict];
if([[self.prefs valueForKey:#"debugMode"] isEqualToString:#"Y"])
{
NSLog(#"error: %#", error);
}
return NO;
}
//END:progressivelyMigrateURLFindMap
//We have a mapping model and a destination model. Time to migrate
//START:progressivelyMigrateURLMigrate
NSMigrationManager *manager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:targetModel];
// reg KVO for migration progress
[manager addObserver:self forKeyPath:#"migrationProgress" options:NSKeyValueObservingOptionNew context:NULL];
NSString *modelName = [[modelPath lastPathComponent] stringByDeletingPathExtension];
NSString *storeExtension = [[sourceStoreURL path] pathExtension];
NSString *storePath = [[sourceStoreURL path] stringByDeletingPathExtension];
//Build a path to write the new store
storePath = [NSString stringWithFormat:#"%#.%#.%#", storePath, modelName, storeExtension];
NSURL *destinationStoreURL = [NSURL fileURLWithPath:storePath];
if (![manager migrateStoreFromURL:sourceStoreURL type:type options:nil withMappingModel:mappingModel toDestinationURL:destinationStoreURL destinationType:type destinationOptions:nil error:&error])
{
if([[self.prefs valueForKey:#"debugMode"] isEqualToString:#"Y"])
{
NSLog(#"error: %#", error);
}
[targetModel release];
[manager removeObserver:self forKeyPath:#"migrationProgress"];
[manager release];
return NO;
}
[targetModel release];
[manager removeObserver:self forKeyPath:#"migrationProgress"];
[manager release];
//END:progressivelyMigrateURLMigrate
//Migration was successful, move the files around to preserve the source
//START:progressivelyMigrateURLMoveAndRecurse
NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString];
guid = [guid stringByAppendingPathExtension:modelName];
guid = [guid stringByAppendingPathExtension:storeExtension];
NSString *appSupportPath = [storePath stringByDeletingLastPathComponent];
NSString *backupPath = [appSupportPath stringByAppendingPathComponent:guid];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager moveItemAtPath:[sourceStoreURL path] toPath:backupPath error:&error])
{
if([[self.prefs valueForKey:#"debugMode"] isEqualToString:#"Y"])
{
NSLog(#"error: %#", error);
}
//Failed to copy the file
return NO;
}
//Move the destination to the source path
if (![fileManager moveItemAtPath:storePath toPath:[sourceStoreURL path] error:&error])
{
if([[self.prefs valueForKey:#"debugMode"] isEqualToString:#"Y"])
{
NSLog(#"error: %#", error);
}
//Try to back out the source move first, no point in checking it for errors
[fileManager moveItemAtPath:backupPath toPath:[sourceStoreURL path] error:nil];
return NO;
}
//We may not be at the "current" model yet, so recurse
return [self progressivelyMigrateURL:sourceStoreURL ofType:type toModel:finalModel];
//END:progressivelyMigrateURLMoveAndRecurse
}
This is an edited version of a method I got from some Core Data book I can't remember the title of. I wish I could give credit to the author. :S
Beware, I have some code in here that you should remove in your implementation. It's mostly stuff I use to update the view on the progress of the migration.
You can use this method like so:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"YongoPal.sqlite"];
// perform core data migrations if necessary
if(![self progressivelyMigrateURL:storeURL ofType:NSSQLiteStoreType toModel:self.managedObjectModel])
{
// reset the persistent store on fail
NSString *documentDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:[NSBundle pathForResource:#"YongoPal" ofType:#"sqlite" inDirectory:documentDir] error:&error];
}
else
{
NSLog(#"migration succeeded!");
}
Remember to remove the lightweight migration option before you use this.
Apple's Core Data Model Versioning and Data Migration Programming Guide addresses what to do if "you have two versions of a model that Core Data would normally treat as equivalent that you want to be recognized as being different", which I think is what you're asking.
The answer is to set the versionHashModifier on one of your Entities or Properties in the new model.
In your example, you'd do that on the field whose meaning has changed.

Core Data : Post migration, additional migration code

I wish to migrate from my version1 data model to version2, but once the migration is complete I wish to perform some custom migration code. How will I know if/when the migration occurs? Is there a migrationHasCompleed delegate method or notification?
For interests sake: The custom migration code I wish to perform resizes png's in the database.
For reference, you can also test in advance whether a migration is necessary, which would probably be cleaner.
NSError *error;
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType
URL:storeURL
error:&error];
NSManagedObjectModel *destinationModel = [persistentStoreCoordinator managedObjectModel];
BOOL migrationRequired = ![destinationModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata];
// Now add persistent store with auto migration, and do the custom processing after
Well, you could run this code right after the migration happen, on your persistent store coordinator setup:
+ (NSPersistentStoreCoordinator *)persistentStoreCoordinatorForStore:(NSString *)store {
NSURL *storeUrl = [NSURL fileURLWithPath:[[[self class] applicationDocumentsDirectory] stringByAppendingPathComponent:store]];
NSError *error = nil;
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[self class] managedObjectModel]];
// Check for model changes without trying to update
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
// Set the automatic update options for the current model
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
NSLog(#"Error opening the database. Deleting the file and trying again.");
//delete the sqlite file and try again
[[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object model
Check the error message to determine what the actual problem was.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
} else { // The model was updates successfuly
// Implement here your custom migration code
}
}
return [persistentStoreCoordinator autorelease];
}
Cheers,
vfn

Core Data Lightweight Migration - Cant Merge Models

I need to add some attributes to my core data model and I am having a terrible time getting lightweight migration to work!
I keep getting the error "Cant merge models with two different entities named blah".
Here's what I've done ...
Added this code to my app delegate.
(NSPersistentStoreCoordinator*)persistentStoreCoordinator {
//blah blah
NSDictionary* options =
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
//blah blah
return _persistentStoreCoordinator;
}
Clicked on my data model, went to Design > Data Model > Add Model Version.
Made my changes to the one with the LOWEST number, basically adding a few attributes.
Deleted all the managed files produced from my previous model, sent them to trash, then created new ones from the new model.
Cleaned all targets.
Build and go.
ERROR.
Please please help. I've tried the above in numerous different ways, and loads of other stuff, each time going back to a clean copy of my project and starting again, and nothing has got me past this error.
Thanks!
Well, once again, another 6 hours of my life completely wasted because Apple are a bunch of ... well, I'll stop there.
Anyway, thanks to this lovely person: http://linkroller.com/fullpage/ad/13754/?http://iphonedevelopment.blogspot.com/2009/09/core-data-migration-problems.html I was able to solve the problem.
You follow the steps I already followed, then you need to find the following method:
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return managedObjectModel;
}
and change it to:
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"Foo" ofType:#"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
return managedObjectModel;
}
where foo is the name of you xcdatamodeld file.
AAAAAARGH.
I had fixed the core data migration
pls follwing this steps
Go AppDelegate.m write function
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSString *databaseFilePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"sampleiOS.sqlite"];
NSURL *storeUrl = [NSURL fileURLWithPath: databaseFilePath];
NSDictionary *_option = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:_option error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
}
return __persistentStoreCoordinator;
}
then select example.xcdatamodeld file
click the Editor menu on the top => add model version => create the new model version "example 2.xcdatamodel" then click finish button.
now show two core data model version one is source "example.xcdatamodel" another one is destination "example 2.xcdatamodel".
now add an attribute or entity in your new version datamodel "example 2.xcdatamodel". then click the group data model "example.xcdatamodeld". After that set current version to be newly created data model "example 2.xcdatamodel".
How to set current Version
select show utilities => show inspector => versioned core data model . then set current version.

Core Data migration failing with error: Failed to save new store after first pass of migration

In the past I had already implemented successfully automatic migration from version 1 of my data model to version 2. Now, using SDK 3.1.3, migrating from version 2 to version 3 fails with the following error:
Unresolved error Error Domain=NSCocoaErrorDomain Code=134110 UserInfo=0x5363360 "Operation could not be completed. (Cocoa error 134110.)", {
NSUnderlyingError = Error Domain=NSCocoaErrorDomain Code=256 UserInfo=0x53622b0 "Operation could not be completed. (Cocoa error 256.)";
reason = "Failed to save new store after first pass of migration.";
}
I have tried automatic migration using NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption and also migration using only NSMigratePersistentStoresAutomaticallyOption, providing a mapping model from v2 to v3.
I see the above error logged, and no object is available in the application. However, if I quit the application and reopen it, everything is in place and working.
The Core Data methods I am using are the following ones
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"MYAPP" ofType:#"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
return managedObjectModel;
}
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"MYAPP.sqlite"]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Handle error
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
}
return persistentStoreCoordinator;
}
In the simulator, I see that this generates a MYAPP~.sqlite files and a MYAPP.sqlite file. I tried to remove the MYAPP~.sqlite file, but
BOOL oldExists = [[NSFileManager defaultManager] fileExistsAtPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"MYAPP~.sqlite"]];
always returns NO. Any clue? Am I doing something wrong?
Thank you in advance.
I ran into this as well and after reading as much Apple docs and web postings as I could find there didn't seem to be an answer. In my case the manual migration was working as well but when I went to open up a new coordinator it would give the same error you had. I finally decided to go back to my last working version of the data model and do a series of small changes/versions and see where it broke the auto-migration capabilities to drill further into it and it turned out it didn't. Now I can add entities, attributes and relationships without issue and they auto-migrate. Any chance you deleted an interim version of the datamodel?
For what it's worth, the Magical Record Core Data utility package includes this hack:
[coordinator MR_addAutoMigratingSqliteStoreNamed:storeFileName];
//HACK: lame solution to fix automigration error "Migration failed after first pass"
if ([[coordinator persistentStores] count] == 0)
{
[coordinator performSelector:#selector(MR_addAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
}
You might try something similar. I have been unable to find an explanation of what the problem is, though, or why just retrying it would work.