Problems inserting new record in Core Data

0

I noticed a problem with the code, when I try to insert a new record into an entity, I lose all the old records and only the new one gets saved.

Code used:

NSManagedObjectContext * context = [self managedObjectContext];

NSManagedObjectContext * novoObjeto = [NSEntityDescription insertNewObjectForEntityForName: entidadeBebidas inManagedObjectContext: context];

[novoObjeto setValue: novoValor forKey: @ "id"];

NSError * error = nil;

if (![context save: & error]) {
    //[self alertaErroCadastro];
    //NSLog(@"Erro ao salvar no banco local");
}

Before insertion:

| id |
12542
12542
25412
21452

After inserting the new record:

| id |
998698

Previous records are lost.

    
asked by anonymous 09.02.2015 / 21:47

1 answer

1

I discovered the error, there was if within the body of the method that would decide if it was increment and decrement of record.

The Problem, first block of if modified some variables that made the condition True to enter the second if and removed the values in an inverted way.

I fixed the code and isolated the functions in different methods. And I removed the reference for the same variables.

    
20.02.2015 / 12:56