I am trying to edit a item in one of my coredata
tables/entities. I am not 100% sure how to do this but I think its along these lines.
First you create the context, then fetchrequest
the entity using a predicate to select the exact item from the table/entity. then save these values into the correct var type update the values then some how overwrite the existing item with the new values.
The last part is where I am stuck, this is the code I currently have:
- (void)editSelectedInstall:(NSString *)invGuid {
NSManagedObjectContext *context = [self managedObjectContext];
if (context == nil) {
NSLog(@"Nil");
}
else {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Install" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"invGUID==%@",invGuid];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *myArray = [context executeFetchRequest:fetchRequest error:&error];
NSMutableDictionary *myDictionary = [myArray objectAtIndex:0];
NSLog(@"%@", myDictionary);
// Here I will update the values & then save the context?
// For example I think I am to update the items like this:
myDictionary.num = @"1234";
myDictionary.name = @"new name";
}
}
I think I'm almost there I just need help saving the context so that it overwrites the previous values.
Try this:
//loop through result set and update as required
for (Install *temp in myArray) {
temp.num = @"1234";
temp.name = @"New name";
}
//save
[context save:&error];