Newbie needing help with editing / updating object properties (attributes) into Coredata. I am able to persist new objects into the store and delete existing ones, but just unable to edit /update existing ones.
Basically the attributes of the entity are populated in a mainTableViewController and user taps on the selected row will segue to detailViewController and populates the data in to textfields, images and textview...
if the user choose to edit the data, it will segue to editViewController and user can edit the data directly from the textFields, textView which was populated from the detailViewController.
Based on the code below, I am unable to edit the entity attributes . Please assist. Thank you.
- (IBAction)editLoginDoneBarButtonPressed:(UIBarButtonItem *)sender {
NSManagedObjectContext *context = [CoreDataHelper managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Login" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"loginid == %@", self.loginIDEditVC];
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"loginid" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
Login *loginUpdate = [[context executeFetchRequest:request error:&error] objectAtIndex:0];
//edit our objects in the Model (ManagedObjectModel)
loginUpdate.name = self.editLoginNameTextField.text;
loginUpdate.image = self.editLoginImageView.image;
loginUpdate.login = self.editLoginUsernameTextField.text;
loginUpdate.password = self.editLoginPasswordTextField.text;
loginUpdate.url = self.editLoginURLTextField.text;
loginUpdate.notes = self.editLoginNotesTextView.text;
if (![context save:&error]) {
NSLog (@"Can't update and save! %@ %@", error, [error localizedDescription]);
}
//we need to unwindsegue
[self performSegueWithIdentifier:@"returnToLoginVC" sender:self]; }