4

我想重新创建(删除和创建)persistentStore,以免受到更改 .xcdatamodeld 的影响。

我在 AppDelegate 中编写了一个代码 persistentStoreCoordinator,如下所示:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

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

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myproject.sqlite"];

    // delete if database exists
    NSError *error = nil;
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        // if .xcdatamodeld is changed, fail and in here...
        // if not changed, recreate success. all data removed from database

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    NSArray *stores = [_persistentStoreCoordinator persistentStores];
    for (NSPersistentStore *store in stores) {
        [_persistentStoreCoordinator removePersistentStore:store error:nil];
        [[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:nil];
    }

    // newly create database
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}

当我对.xcdatamodeld(例如,向实体添加新列)进行更改并重新启动模拟器时,首先失败addPersistentStoreWithType并记录说

Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 
The operation couldn’t be completed. (Cocoa error 134100.)

我怎样才能做到这一点?

4

2 回答 2

1

The following code seems to be work nice for me.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

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

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myproject.sqlite"];

    // delete database if exists
    [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:nil];

    // create database
    NSError *error = nil;
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}

I checked below (in a device and simulator):

  1. When first boot, database should be created
  2. Edit .xcdatamodeld (add/edit/remove a column or an entity) then reboot, database should be recreated

Thanks for your advice.

于 2013-12-02T16:51:51.547 回答
1

解决您的问题的最简单方法是通过以下方式处理此错误:删除您的数据库文件并重试。它可以在测试期间完成。

但是,如果您需要一个稳定的解决方案,请使用带有版本和自动迁移的模型:

[persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType
                                         configuration: nil
                                                   URL: storeURL
                                               options: @{NSMigratePersistentStoresAutomaticallyOption : @(YES),
                                                       NSInferMappingModelAutomaticallyOption       : @(YES)}
                                                 error: &error];

此外,如果自动迁移还不够,您应该提供迁移映射。

如果您只想删除存储,请使用这样的文件管理器:

[[NSFileManager defaultManager] removeItemAtURL: storeURL
                                          error: &error];
于 2013-12-02T09:20:10.830 回答