当我尝试实施迁移时,我的应用崩溃了。
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
即使启用了NSZombie并且我在胎面堆栈中获得了exec_bad_access
- (NSManagedObjectContext *)managedObjectContext
{
if ( _managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = nil;
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
// NSString *path = [[NSBundle mainBundle] pathForResource:@"ToGoNow" ofType:@"momd"];
// NSURL *modelURL = [NSURL fileURLWithPath:path];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"ToGoNow" withExtension:@"momd"];
NSError *error = nil;
if ([modelURL checkResourceIsReachableAndReturnError:&error] == NO){
NSLog(@"Error: %@", error); //[[NSAlert alertWithError:err] runModal];
}
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ToGoNow.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if ([storeURL checkResourceIsReachableAndReturnError:&error] == NO){
NSLog(@"Error: %@", error); //[[NSAlert alertWithError:err] runModal];
}
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(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
要解决此问题,我尝试了以下操作 1.删除版本化模型
导入模型的旧副本(不进行版本控制)运行应用程序(在模拟器中运行正常)
添加新模型版本
将int16类型的新属性添加到现有实体
使用新的nsinteger属性将相应的NSManagedObject类更新为该实体
重新运行应用程序,但仍然崩溃:
if(![_ persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:& error])
我也已经转向核心数据调试,这是我在崩溃后得到的:
2014-01-11 18:34:28.233 ***[17494:70b] CoreData: annotation: Connecting to sqlite database file at "/Users/***/Library/Application Support/iPhone Simulator/7.0.3/Applications/7822B8E6-36B6-4EE5-8A42-253EEA4F3093/Documents/ToGoNow.sqlite"
2014-01-11 18:34:28.235 ***[17494:70b] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA
2014-01-11 18:34:28.237 ***[17494:70b] CoreData: annotation: Disconnecting from sqlite database.
2014-01-11 18:34:28.240 ***[17494:70b] CoreData: annotation: Connecting to sqlite database file at "/Users/***/Library/Application Support/iPhone Simulator/7.0.3/Applications/7822B8E6-36B6-4EE5-8A42-253EEA4F3093/Documents/***.sqlite"
2014-01-11 18:34:28.241 ***[17494:70b] CoreData: sql: pragma journal_mode=wal
2014-01-11 18:34:28.242 ***[17494:70b] CoreData: sql: pragma cache_size=200
2014-01-11 18:34:28.243 ***[17494:70b] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA
`