核心数据迁移在addPersistentStoreWithType处崩溃

时间:2014-01-11 01:59:35

标签: ios core-data-migration

当我尝试实施迁移时,我的应用崩溃了。

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.删​​除版本化模型

  1. 导入模型的旧副本(不进行版本控制)运行应用程序(在模拟器中运行正常)

  2. 添加新模型版本

  3. 将int16类型的新属性添加到现有实体

  4. 使用新的nsinteger属性将相应的NSManagedObject类更新为该实体

  5. 重新运行应用程序,但仍然崩溃:

    if(![_ persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:& error])

  6. 我也已经转向核心数据调试,这是我在崩溃后得到的:

    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
    

    `

0 个答案:

没有答案