Asignar valores a un objeto en datos básicos

A continuación se muestra mi fragmento de código:

NSFetchRequest *request= [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Login" inManagedObjectContext:self.managedObjectContext];
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"userid==%@ AND password==%@",useridentered, passwordentered];
[request setEntity:entity];
[request setPredicate:predicate];
NSError *anyError=Nil;
NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:request error:&anyError];

Login *login = [fetchedObjects objectAtIndex:0];
Information *information = [NSEntityDescription insertNewObjectForEntityForName:@"Information" inManagedObjectContext:self.managedObjectContext];
login.information = information;
login.information.title = informationTitleTextView.text;
login.information.info_1 = information1textview.text;
login.information.info_2 = information2textview.text;

[self.managedObjectContext save:nil];  // write to database
NSLog(@"login.information %@",login.information.title);
NSLog(@"login.title %@", login.userid);
   [self.delegate savebuttontapped:self];

The problem is, it works fine for the first time. But when i again add information for the same user it overwrites the existing one. How to make sure that it gets written to next line?

preguntado el 10 de marzo de 12 a las 07:03

@iamsult what formatting should i do? -

Are you sure that you want to save passwords into a DB? -

1 Respuestas

You are creating a new managed object for the information (new DB row):

Information *information = [NSEntityDescription insertNewObjectForEntityForName:... inManagedObjectContext:...];

And then you assign it to the user.

login.information = information;

This does not remove the old information from the DB, but the user loses relationship with it.

I suppose that what you want is having more than one Information instances connected to one Login instance. That means you have to open your Managed Object Model and change the one-to-one relationship into one-to-many relationship.

Entonces tu código será:

[login.informations addObject:information];

or

information.login = login;

respondido 10 mar '12, 08:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.