0

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];    }
mikebruno
  • 33
  • 2
  • possible duplicate of [How to update existing object in Core Data?](http://stackoverflow.com/questions/10571786/how-to-update-existing-object-in-core-data) – Lorenzo B May 13 '14 at 21:07
  • In what way does this code not work? – Tom Harrington May 14 '14 at 02:11
  • loginUpdate is non-nil? And don't you get a warning about the assignment to it? – stevesliva May 14 '14 at 03:09
  • @TomHarrington when the existing attributes are edited and once the done button is tapped. The attributes are not updated. In the mainLoginTableViewController , the attributes remain the same as if there was no editing done. – mikebruno May 14 '14 at 03:34
  • @stevesliva no warning. – mikebruno May 14 '14 at 03:36
  • @flexaddicted looks similar to your example. Let me try changing mine to suit yours. – mikebruno May 14 '14 at 03:41
  • @flexaddicted Yes it worked. Thanks. But funny thing though, when i edit the loginObjects in the mainViewController the loginObject is updated, however, it duplicates itself onto another existing loginObject and the loginObject that was selected to be edited (remains unedited in the same tableviewcellrow)..that means the duplication did not create a new loginObject but merely deleted another while the loginObject count remains the same. Have you experienced something like this before? – mikebruno May 14 '14 at 16:38

0 Answers0