1
votes

I think the answer is very simple, maybe I'm missing something important! I can insert a new object, I can remove an object from managerObjectContext, I can access the object via its ID, but I can not understand how I can change it!

my code is very simple:

- (NSManagedObjectContext *)managedObjectContext {
return [(CalcAppDelegate*)[[UIApplication sharedApplication]delegate] managedObjectContext];}


- (void)viewDidLoad {
[super viewDidLoad];

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Materials"];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]];

list = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];

self.navigationItem.rightBarButtonItem = self.editButtonItem;

[tbl reloadData];}

i pass data to another ViewController

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"MatListToDetailSegue"]) {
    MaterialsDetailViewController *detailMaterial = segue.destinationViewController;
    detailMaterial.material = [list objectAtIndex:tbl.indexPathForSelectedRow.row];
    detailMaterial.managedObjectId = [[list objectAtIndex:tbl.indexPathForSelectedRow.row] objectID];
}

and i want to save this managedObject in managedObjectContext and commit the managedObjectContext , but i don't have any changes in SQLite file!

- (IBAction)saveBtn:(id)sender {

material.matName = editMaterialName.text;
material.matWidth = [NSNumber numberWithInt:300];
material.matPrice = [NSNumber numberWithInt:300];

[self.managedObjectContext save:nil];
[self.navigationController popViewControllerAnimated:YES];}

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Materials''

wow! thank you vary mach for the answers! i solved the problem.

Al i needed to: delete this from method -(IBAction)saveBtn

material = (Materials *)[NSEntityDescription entityForName:@"Materials" inManagedObjectContext:managedObjectContext];

and change [self.managedObjectContext save:nil];

NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
}

Thanks for advance for your reply!!!

1
At first, you're not checking return value of -[NSManagedObjectContext save:] which is BOOL. If it returns NO, nothing was saved. Also you're not retrieving error in case when it returns NO. Fix it and come later with error message. - zrzka
thank you, i fix it and now i have error when i call the method -(IBAction)saveBtn:(id)sender - Aleks Kor
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Materials'' - Aleks Kor
create a "find" method that will fetch/create a new material object from db, modify that object and save to it's own context. - Calin Chitu
Are you sure you have an entity that is called Materials? Because that is what the exception is telling you is wrong. - Abizern

1 Answers

0
votes

Wait, into your Material object you are assigning an instance of NSEntityDescription. I am almost certain that is not what you want.

That is why you are crashing. When Core Data is saving, it is trying to save your Material object (expecting it to be a subclass of NSManagedObject), but instead you have a NSEntityDescription in that memory space.

Also, you are somewhere using both Materials and somewhere Material. Can't that also be a problem?