How to recreate persistentStore so as not to affect .xcdatamodeld change

I would like to recreate (delete and create) the persistentStore so as not to affect the .xcdatamodeld change.

I wrote the code persistentStoreCoordinator in the AppDelegate application 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 changes to .xcdatamodeld(e.g. adding a new column to the entity) and the reload simulator, then crash first addPersistentStoreWithTypeand log

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

How can i do this?

+4
source share
2 answers

The easiest way to solve your problem is to handle this error as follows: delete your DB file and try again. This can be done for a testing period.

, , :

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

, , .

, :

[[NSFileManager defaultManager] removeItemAtURL: storeURL
                                          error: &error];
+1

.

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

( ):

  • .xcdatamodeld(// ), ,

.

+1

All Articles