Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Monaj

macrumors regular
Original poster
May 24, 2009
193
0
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:

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:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.