Foreign Key Relationship Mapping with Restkit

Foreign key relationship mapping with RestKit

You need to add a transient attribute to your User entity which holds the associated teamId. This needs to be added to your userMapping.

Then, you need to add a relationship definition to your userMapping:

[userMapping addConnectionForRelationship:@"team" connectedBy:@{ @"teamId": @"teamId" }];

This gives RestKit the information it needs to make the connection and instructs it to make the connection as part of the mapping operation.

RestKit: Creating stubs for foreign key relationships

I thought my below answer previously worked, but maybe I always snuck around the issue in the comments about exact matches of response descriptors.

So, create another mapping, but make it a nil keypath mapping and use a keypath on the response descriptor of @"teamId". This will cause RestKit to extract an array of team ids from the response and process them as object items, separate to the user objects.


The JSON is fine. Just create a new response descriptor which uses a teamStubMapping, which is a new mapping like teamMapping but replacing id with teamId. The path pattern of this response descriptor should be @"Users".

Restkit - Core Data One-To-One Relationship Mapping via Foreign Key

You describe 2 different relationships, but really you just have one with an inverse. This is correct, it's just your description which is wrong (and thus probably your understanding of setting the relationship contents).

The connection cannot be made (and you get an error) because RestKit needs to know the id of the other object to find (to connect to). You're telling it to use location_id, but it needs to be a property on the class (usually transient), not an item in the JSON (because the connection is made after the JSON has been processed and gone).

Add a property to the entity and a mapping to place the location_id into it. Update the connection with the name of that property.

RestKit - Core Data Many-to-Many Relationship via Nested Foreign Keys

You can't use the foreign key capability of RestKit for serialisation because it's designed for mapping incoming data. I haven't tried, but I wouldn't expect the connection to be reversed when taking the inverse of a mapping, because it is just using the information from other mappings (usually to transient variables) to facilitate relationship establishment.

So, what you should be doing is creating a custom mapping (rather than an inverse) which extracts only the information you want from the relationship (the identity) and using that mapping for your serialisation.

Restkit use @parent in nested Foreign Key relationship

Just don't do what you're trying to do. Your mappings look fine and, assuming you have the inverse relationships configured in the model, you can just navigate the relationships from the item entity to get to the associated venue entity.

That said, I think the @parent.id reference should work to make the connection possible (by giving access to the id). But again, really not required. You can fetch and predicate on the model to get whatever information you need with the relationships you have.

RestKit object mapping with foreign keys and services

RestKit won't automatically load the data for the relationship, you need to tell it when to do so. If you add the route using routeWithRelationshipName:objectClass:pathPattern:method: then you can make the request using getObjectsAtPathForRelationship:ofObject:parameters:success:failure:. Basically it makes linking the relationship name to the request to gather the required details.

Restkit: Stubs for foreign key relationships

It looks like you want something along the lines of:

let userMapping = RKEntityMapping(forEntityForName: "User", inManagedObjectStore: mos)
userMapping.identificationAttributes = ["userId"]
userMapping.addAttributeMappingsFromDictionary([
"id": "userId",
"name": "firstName",
"surname": "lastName"
])

let teamStubMapping = RKEntityMapping(forEntityForName: "Team", inManagedObjectStore: mos)
teamStubMapping.addPropertyMapping(RKAttributeMapping(fromKeyPath: nil, toKeyPath: "teamId"))
teamStubMapping.identificationAttributes = ["teamId"]

userMapping.addRelationshipMapping(sourceKeyPath: "teams", mapping: teamStubMapping)

RestKit Core Data mapping connection with foreign key in the URL

You can use the foreign key as described in the reference you link, you just need to get it...

To get it, you need to use a path pattern and metadata mapping. You also want to use RKRoute as it does a lot of the work with path patterns.

Define your path pattern like:

projects/:identity/members

And then in your mapping you can define the foreign key to pick this up:

@"@metadata.routing.parameters.identity": @"foreignKey",

See the 'Metadata Mapping' section of the RKObjectManager docs.



Related Topics



Leave a reply



Submit