Problems inserting new registration to Core Data

Asked

Viewed 60 times

0

I noticed a problem with the code, when I try to insert a new record in an entity, I lose all the old records and only the new one is 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.

  • I played here and the insertion worked smoothly, you could post more details?

  • Well I’m using this code inside a looping for

  • Tried to change the type of new NSManagedObject?

  • Thanks for the suggestion... I’ll test now and I’ll tell you if it worked...

1 answer

1


I discovered the mistake, there was a if within the body of the method that decided if it was increment and decrease of record.

The Problem, first block of the if modified some variables that made the condition True to enter the second if and remove the values in reverse.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.