1

每次我向 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;
}

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

编辑:添加了错误。

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;
}

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

4

2 回答 2

4

您需要添加NSMigratePersistentStoresAutomaticallyOption&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];
}

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

在此处阅读有关模型迁移的苹果文档。

于 2013-05-21T10:55:21.337 回答
0

根据错误,这似乎非常明显,但是您需要注意原始数据存储是在比当前版本的 Xcode 更早的版本上创建的实例。

在我的情况下不需要删除的解决方案,但包括更新商店的选项是一种享受(通过 NSMigratePersistentStoresAutomaticallyOption)

于 2014-01-09T17:28:51.133 回答