Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I be associating Core Data entities for use with RestKit?

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!

like image 764
nfm Avatar asked May 26 '11 05:05

nfm


People also ask

How does Core Data work in iOS?

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.

When should I use Core Data?

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.

What is transformable in Core Data?

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.

Do I need Core Data?

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.


1 Answers

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];
like image 73
Evan Cordell Avatar answered Sep 19 '22 14:09

Evan Cordell