I’m sort of new to programming with CoreData, so I have no idea how to go about doing this. I’m trying to update a string that’s being stored in a CoreData object, but I have not the faintest idea on how to do that. Here’s my code:
Here’s where I make the new object:
- (void)insertNewObject:(id)sender
{
self.steakName = @"No Name";
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newManagedObject setValue:self.steakName forKey:@"steakName"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
Here’s where I want to update it: (It’s being updated in a different class then where it’s being made.)
- (IBAction)update:(id)sender {
MasterViewController *masterViewController = [[MasterViewController alloc] init];
masterViewController.steakName = [self.steakNameTextField text];
self.detailDescriptionLabel.text = masterViewController.steakName;
NSLog(masterViewController.steakName);
}
Thank you for your help! 🙂
You do it almost the same way you would without Core Data. Keep a reference to the object as a property in the class where it’s created and make whatever changes you want. The only difference is, when you want the changes to persist, you save your context again.