0

I have a core-data with 2 entities: 'CarEntity' and 'PassengerEntity'.

Now, each entity has an attribute called 'name'. CarEntity has a to-many relationship with PassengerEntity called 'passengers' and PassengerEntity has the inverse relationship 'inCar'.

So, I did this interface to insert a new passenger.

I have one NSTextField for the person's name and one NSPopUpButton to chose the car. The popup button has a "content values" binding to a NSControllerArray that allows me to get all the cars.

Then I have one button to save everything. The header code goes like this:

IBOutlet NSTextField   *newPassengerNameField;
IBOutlet NSPopUpButton *newPersonCarField;

And the implementation goes like this:

- (IBAction)saveNewPassenger:(id)sender {

    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

    NSManagedObject *newPassengerObject = [
        NSEntityDescription
            insertNewObjectForEntityForName:@"PassengerEntity"
                     inManagedObjectContext:managedObjectContext
    ];

    //Here we have all the fields to be inserted.
    [newPassengerObject setValue:[newPassengerNameField stringValue] forKey:@"name"];

    //Car ?????

}

Okay, this code works just fine.. for the name. But I can't figure out how to insert the car relationship.

My application forces the user to create a car before coming to this stage, so I have objects in the CarEntity.

The question is: How do I get the value of the popup button and send it to this insert code?

Thanks!

Apollo
  • 1,913
  • 2
  • 19
  • 26

2 Answers2

0

This pretty much works, but any help in the sense of cleaning the code is much appreciated!

[newPassengerObject setValue:[newPassengerNameField stringValue] forKey:@"name"];

//Continuing from old code.

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name = %@", [[newPersonCarField selectedItem] title]]; 

NSEntityDescription *modelEntity = [NSEntityDescription entityForName:@"CarEntity" inManagedObjectContext:managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setEntity:modelEntity];
[fetchRequest setPredicate:predicate];

NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:nil];

[fetchRequest release];

[newPassengerObject setValue:[results objectAtIndex:0] forKey:@"inCar"];
//Code goes on from here.
Apollo
  • 1,913
  • 2
  • 19
  • 26
0

// Code cleaning comments

1: Why do you use 'setValue:forKey'? Haven't you auto generated your Entity class files. If you had generated, you could have treated entities like custom objects. For eg. I am picking up your piece of code. It could be modified like below, if you had Entity class files

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

PassengerEntity *newPassengerObject = [
    NSEntityDescription
        insertNewObjectForEntityForName:@"PassengerEntity"
                 inManagedObjectContext:managedObjectContext
];
newPassengerObject.name = [newPassengerNameField stringValue];

2: Don't mix up core data code in your saveNewPassenger: method. It was not meant for Core Data operations. In that case, why do you write COre data code inside that method? It will only result in repetition of code. Better write an 'Interface Class' for communicating with Core Data. This will increase modularity and code reusability

Advaith
  • 1,087
  • 3
  • 12
  • 31