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!!!
-[NSManagedObjectContext save:]which isBOOL. If it returnsNO, nothing was saved. Also you're not retrieving error in case when it returnsNO. Fix it and come later with error message. - zrzka