How to recreate persistentStore not to be affected by changing .xcdatamodeld

354 views Asked by At

I'd like to recreate (drop & create) persistentStore not to be affected by changing .xcdatamodeld.

I wrote a code persistentStoreCoordinator in AppDelegate likes below:

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

when I make a change to .xcdatamodeld (ex. adding new column to entity) and reboot simulator, then fail at first addPersistentStoreWithType and log says

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

How can I do that?

2

There are 2 answers

1
eagle.dan.1349 On

The simplest way to solve your problem is to handle this error the following way: delete your DB file and try again. It can do for testing period.

However, if you need a stable solution, use model with version and automatic migrations:

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

Also, if automigrations are not enough, you should provide migration mapping.

If you want just to delete the store use the file manager like that:

[[NSFileManager defaultManager] removeItemAtURL: storeURL
                                          error: &error];
3
bekkou68 On

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.