每当我向Core Data文件添加内容时,我都会获得sigabrt

时间:2013-05-21 10:47:20

标签: ios objective-c core-data sigabrt nspersistentstore

每当我向CoreData添加内容时(比如向其中一个实体添加属性),我得到了sigabrt,唯一有用的是从模拟器中删除应用程序并清理项目。 我添加了一个异常断点,这是断开的函数:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator == nil) {
        NSURL *storeURL = [NSURL fileURLWithPath:[self dataStorePath]];

        _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

        NSError *error;
        if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
            NSLog(@"Error adding persistent store %@, %@", error, [error userInfo]);
            abort();
        }
    }
    return _persistentStoreCoordinator;
}

如果我取消注释中止(),该应用程序可以工作,但它不会访问数据,我该如何解决这个问题,以便我可以添加属性而无需每次都删除所有内容?

编辑:添加错误。

2013-05-21 13:52:35.441 Game[23595:c07] Error adding persistent store Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x7d6e5f0 {metadata={
    NSPersistenceFrameworkVersion = 419;
    NSStoreModelVersionHashes =     {
        GameTypeItem = <9b95d5f6 f27d2c7d 73452e34 c17e63a1 64bb657e 847085b3 12c5d3e0 17ea16b9>;
        GameTypeLevelItem = <3224738f 99c7c8cf 4b23908a 356e345a 8b5d708a f68b7a2c f9de9ccb 0cec8fb8>;
        SoundItem = <eb8cc3cf 0d6b83b4 8c01bb5b 3d2dc6a2 3688577c 4d73e2f4 7742c00e 56fd78de>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "F6FA6ED3-5663-4075-9D17-B38E4497468D";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}, {
    metadata =     {
        NSPersistenceFrameworkVersion = 419;
        NSStoreModelVersionHashes =         {
            GameTypeItem = <9b95d5f6 f27d2c7d 73452e34 c17e63a1 64bb657e 847085b3 12c5d3e0 17ea16b9>;
            GameTypeLevelItem = <3224738f 99c7c8cf 4b23908a 356e345a 8b5d708a f68b7a2c f9de9ccb 0cec8fb8>;
            SoundItem = <eb8cc3cf 0d6b83b4 8c01bb5b 3d2dc6a2 3688577c 4d73e2f4 7742c00e 56fd78de>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "F6FA6ED3-5663-4075-9D17-B38E4497468D";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}

编辑:将功能更改为:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator == nil) {
        NSURL *storeURL = [NSURL fileURLWithPath:[self dataStorePath]];
        _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
        NSError *error;

        if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error])
        {
            [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
            NSLog(@"Deleted old database %@, %@", error, [error userInfo]);
            [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES} error:&error];
            abort();
        }
    }
    return _persistentStoreCoordinator;
}

现在它运行程序但它仍然没有显示数据

2 个答案:

答案 0 :(得分:4)

您需要添加NSMigratePersistentStoresAutomaticallyOption&amp; NSInferMappingModelAutomaticallyOption选项:

试试这个:

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error]) {
    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
    NSLog(@"Deleted old database %@, %@", error, [error userInfo]);
    [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES} error:&error];
}

注意:您需要先打开模型版本控制,并确保每次更改时都创建新版本的数据模型。

阅读有关模型迁移的{apple}文档here

答案 1 :(得分:0)

基于错误似乎非常明显,但您需要满足原始数据存储在早期版本上创建的实例,而不是当前版本的Xcode。

在我的案例中删除的解决方案不是必需的,但包括更新商店的选项有效(通过NSMigratePersistentStoresAutomaticallyOption)