Programmatically adding to a Core Data entity (like IB binding 'add')

2.5k Views Asked by At

I have a core data model with an entity called clients that is made up of the following attributes:

client model entity

If I click on an 'add client' button and bring up the following window:

client window

what would be the correct method to programmatically add a new entry for each property simultaneously (in a similar fashion to how a bound IB 'add' button would work with an NSArrayController) and have them appear in the textfields of the 'add client' window to edit? The textfields in the 'add client' window are bound to the corresponding properties (with one or two still missing) of the client entity. The code I have at the moment is:

- (IBAction)addNewClient:(id)sender;
{
    [addClientsWindow makeKeyAndOrderFront:self];
    //NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
    //[clientsMoc addObject:[clientsMoc newObject]];
    [clientsController addObject:[clientsController newObject]];
}

Which worked for other entities in this project but isn't working for client since I added relationship types (it throws up KVC errors in the console). I imagine it's because I am addressing the NSArrayController rather than the NSManagedObjectContext but the commented out code isn't working for me. This attempt follows on from a previous question as the question has changed a lot and I'm struggling to implement the advice given. I really need a good starting point and the apple dev docs aren't helping me make sense of this.

Thanks in advance!

---- Update ----

Am I explaining things badly in this question? I'm new here but thought there might be an attempt at an answer. Googling this was hard for some reason. I may have found something similar to a solution through an unrelated Google search. Luckily it was relevant to this.

- (IBAction)addNewClient:(id)sender;
{
    [addClientsWindow makeKeyAndOrderFront:self];
    NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
    NSManagedObject *clientsEntity = [NSEntityDescription
                                     insertNewObjectForEntityForName:@"Clients" 
                                       inManagedObjectContext:clientsMoc];
    [clientsEntity setValue:@"name" forKey:@"clientName"];
    [clientsEntity setValue:@"company" forKey:@"clientCompany"];
    [clientsEntity setValue:@"address" forKey:@"clientAddress"];
    [clientsEntity setValue:@"11111111" forKey:@"clientLandline"];
    [clientsEntity setValue:@"[email protected]" forKey:@"clientEmail"];
}

This created a full new entry for the clients entity - I didn't realise I would have to do an individual value for each property. The KVC errors continued though and I couldn't find a solution at all. Apple dev docs were actually helpful on this one, here, and adding NSBindingDebugLogLevel 1 to the “Arguments to be passed at launch” list gave me details of exactly what was causing the problem. It was an old binding to an NSTableColumn that hadn't been updated.

1

There are 1 best solutions below

0
On BEST ANSWER

This may be the solution I was after:

- (IBAction)addNewClient:(id)sender;
{
    [addClientsWindow makeKeyAndOrderFront:self];
    NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
    NSManagedObject *clientsEntity = [NSEntityDescription
                                     insertNewObjectForEntityForName:@"Clients" 
                                       inManagedObjectContext:clientsMoc];
    [clientsEntity setValue:@"name" forKey:@"clientName"];
    [clientsEntity setValue:@"company" forKey:@"clientCompany"];
    [clientsEntity setValue:@"address" forKey:@"clientAddress"];
    [clientsEntity setValue:@"11111111" forKey:@"clientLandline"];
    [clientsEntity setValue:@"[email protected]" forKey:@"clientEmail"];
}

This created a full new entry for the clients entity - I didn't realise I would have to do an individual value for each property. The KVC errors continued though and I couldn't find a solution at all. Apple dev docs were actually helpful on this one, here, and adding NSBindingDebugLogLevel 1 to the “Arguments to be passed at launch” list gave me details of exactly what was causing the problem. It was an old binding to an NSTableColumn that hadn't been updated.