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!
Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.
Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.
Core Data allows us to store integers, booleans, strings, UUID, date, etc. but sometimes we want to store a specific data type like UIColor, UIImage, our own class, struct, or enum, and even arrays, but that is simply not an option in Attribute's Type.
The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.
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:
DBTopic* topic = [[DBTopic findFirstByAttribute:@"topicID" withValue:topicID] retain];
_post = [[DBPost object] retain];
_post.topicID = topic.topicID;
_post.topic = topic;
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:
RKManagedObjectMapping* clientMapping = [RKManagedObjectMapping mappingForClass: [Client class]];
clientMapping.primaryKeyAttribute = @"clientID";
[clientMapping mapKeyPathsToAttributes:
@"id", @"clientID",
nil];
RKManagedObjectMapping* userFileMapping = [RKManagedObjectMapping mappingForClass:[UserFile class]];
userFileMapping.primaryKeyAttribute = @"userFileID";
[userFileMapping mapKeyPathsToAttributes:
@"id", @"userFileID",
@"client_id", @"clientID",
nil];
[userFileMapping mapRelationship:@"client" withObjectMapping:clientMapping];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With