Search code examples
iosobjective-ccore-dataxcode5

Error with core-data


when i add the autoLogin Attribute on my Entity Utente the code don t work, if i delete this attribute the code work, why??? PS: i needed the new attribute, somebody can help me pls :)

Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x10970aae0 {metadata={
    NSPersistenceFrameworkVersion = 479;
    NSStoreModelVersionHashes =     {
        Utente = ;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "C879290E-F81B-4D22-B6FF-12F34B97820F";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}, {
    metadata =     {
        NSPersistenceFrameworkVersion = 479;
        NSStoreModelVersionHashes =         {
            Utente = ;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "C879290E-F81B-4D22-B6FF-12F34B97820F";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}

code:

 //caricamento DB
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext]; //the error signal is here
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Utente" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];



NSManagedObject *matches = nil;

NSError *error;
NSArray *objects = [context executeFetchRequest:request
                                          error:&error];

if ([objects count] == 0)
{
    NSLog(@"No Email save");
}
else
{
    for (int i = 0; i < [objects count]; i++)
    {
        matches = objects[i];
        //NSManagedObject *o = objects[i];
        //[context deleteObject:o]; //cancella
        //[context save:&error];

        //email
         NSLog(@"email salvata : %@",[matches valueForKey:@"email"]);
        emailText.text=[matches valueForKey:@"email"];


        //password
        NSString * psw =[matches valueForKey:@"password"];
        if (psw)
        {
            NSLog(@"password salvata : %@",[matches valueForKey:@"password"]);
            passwordText.text=[matches valueForKey:@"password"];

            //auto-login
            NSString * psw =[matches valueForKey:@"autoLogin"];
            if (psw)
            {
                [self login:self];
            }
        }

Solution

  • In fact, once you deploy an app which integrates Core Data, the generated model is versioned.

    If you need to add a new attribute to your entity, before, you need to create a new version of your CoreData model, by selecting menu " Editor -> Add Model Version ", and then make your changes on your entities.

    enter image description here

    If your modifications are simple (column deletion, column addition...), CoreData can handle a lightweight migration with not much specific code, but you need to keep in your project ALL VERSIONS of your CoreData model, to enable iOS to perform the migrations for existing stores.

    All is fully detailed in Apple Documentation, which can be found here : https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/CoreDataVersioning/Articles/Introduction.html