I made a sample core data application, in which I populated NSComboBox with name of Student entity, by binding its content values property to an array controller. I wanted to add new record to persistent store if student name does not exist, so I defined this method in application delegate class:
Can anyone suggest me, if it is the best way to implement it or there is any other better approach to implement it?
Code:
- (IBAction)addNewStudent:(id)sender
{
NSArray *studentNames = [[students arrangedObjects] valueForKey:@"name"];
if ([studentNames containsObject:[sender stringValue]]) {
return;
}
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *studentEntity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];
// inserting record
NSManagedObject *newRecord = [NSEntityDescription insertNewObjectForEntityForName:[studentEntity name] inManagedObjectContext:context];
[newRecord setValue:[sender stringValue] forKey:@"name"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@",error,[error userInfo]);
abort();
}
}
Can anyone suggest me, if it is the best way to implement it or there is any other better approach to implement it?
Last edited: