I have two Core Data entities (Client and UserFile) that I have successfully set up a relationship between.
I have created classes for both entities, and made them subclasses of RKManagedObject.
When I create a new UserFile, I want to correctly associate it with a Client. Here’s what I’m doing:
Client *client = [Client objectWithPrimaryKeyValue:@"1"];
UserFile *file = [UserFile object];
file.client = client;
file.clientId = client.clientId;
[[RKObjectManager sharedManager] postObject:file delegate:self];
It seems like I have to assign file.clientId so that the correct parameter is sent to the server (if I only assign file.client then the submitted client_id is blank).
It seems like I have to assign file.client to prevent a new, empty Client from being created and associated with the file (the client relationship is required).
Is this correct? Do I really have to assign both the foreign key and the actual entity? This seems a bit redundant to me, but I’ll happily admit that my Core Data and RestKit knowledge is lacking!
To answer your question, it looks like you do need to to both steps at the moment. Here’s the code from the RKDiscussionBoardExample included with the library:
So either the relationships aren’t set up properly in the example, or you really do need both steps.
Also, you should be using the newest version of RestKit which has a different object mapper and deprecates RKManagedObject. Your relationships should look something like this: