Frage

Every time i add something to the CoreData (like adding an attribute to one of the entities) I get sigabrt and the only thing that helps is deleting the app from the emulator and cleaning the project. I added an exception breakpoint and this is the function that breaks:

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

If I uncomment the abort() the app works but it wont access the data, how can I fix that so I can add an attribute without needing to erase everything every time?

EDIT:added the error.

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

EDIT:Changed the function to this:

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

now it runs the program but it still doesnt show the data

War es hilfreich?

Lösung

You need to add the NSMigratePersistentStoresAutomaticallyOption & NSInferMappingModelAutomaticallyOption options:

Try this:

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

NOTE: you need to turn on model versioning first and make sure that you create a new version of your data model each time you change it.

Read the apple documentation about model migration here.

Andere Tipps

It seems really obvious based on the error, but you need to cater for instances of when the original data store was created on earlier versions than the current version of Xcode.

The solution of deleting in my case wasn't required, but including the option to update the store worked a treat (via NSMigratePersistentStoresAutomaticallyOption)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top