问题
I have added new version of the data model. I added new fields in new version.Also set current Model Version to newly created Version.
Code update:
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"PPUSFAModel" withExtension:@"momd"];
//I earlier was PPUSFAModel 16, i added new model version PPUSFAModel 17.
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:SQLITENAME];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
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]) {
//Error
}
return _persistentStoreCoordinator;
}
It worked until some attributes added but after some point i am getting crash at following code
- (void)saveContext
{
__block NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
[managedObjectContext performBlockAndWait:^{
if (managedObjectContext != nil && _persistentStoreCoordinator != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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.
// DLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
}
} }];
}
This is crashed screenshot:
When i install by removing earlier app from device it works, but getting crash when app update on existing version.
//If i added field in old model version accidentally instead of new version how to fix that?
Any help will appreciated.
回答1:
Please make sure you have updated the model "Current Version". Update "Current Version" in .xcdatamodel -> File Inspector -> Model version
Then go to "AppDelegate.swift" file and add below code:
let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
Then app "options" to below code for parameter "option":
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
The code will look like this: image
回答2:
The migration is failing due to your model changes. Core Data can automatically migrate –
- Simple addition of a new attribute
- Removal of an attribute
- A non-optional attribute becoming optional
- An optional attribute becoming non-optional and defining a default value
- Renaming an entity or property
To determine if Core Data can perform an automatic migration use NSMappingModel’s inferredMappingModelForSourceModel:destinationModel:error:
method. It will return nil if Core Data cannot complete the migration.
When Core Data cannot perform automatic migrations, you must use the Migration Manager. Apple's documentation provides sample code to explain how to use the migration manager.
回答3:
If i added field in old model version accidentally instead of new version how to fix that?
Doing that would account for the problems you're seeing, and for the fact that deleting the app and reinstalling works. What you do is go back and undo that change. Hopefully you're using git or some other version control system. If so, use git to reset the model file to the last working commit (or do the equivalent in your system). Then make the change in the new version instead of the old one.
来源:https://stackoverflow.com/questions/43279438/nspersistentstorecoordinator-has-no-persistent-storesschema-mismatch-or-migrati