Reputation: 2050
I am having an interesting problem with RestKit. I want to be able to map my objects by their uniqueIDs in CoreData. It looks like RKConnectionDescription should allow me to do just that. I want to set up a connection between a RegisteredUser and a BasicModel object. The thing is that when I create the connection I receive an NSInternalInconsistencyException
because RestKit says, Cannot connect relationship: invalid attributes given for source entity 'RegisteredUser'
Below is the code I have to create the mapping for the user.
RKEntityMapping *userEntityMapping = [RKEntityMapping mappingForEntityForName:@"RegisteredUser" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[userEntityMapping addAttributeMappingsFromArray:@[@"uniqueID",
@"createdAt",
@"updatedAt",
@"firstName",
@"middleName",
@"lastName",
@"email",
@"gender",
@"dateOfBirth",
@"profileImageUpdatedAt"]];
NSEntityDescription *userEntity = [NSEntityDescription entityForName:@"RegisteredUser" inManagedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext];
NSRelationshipDescription *basicModelRelationship = [userEntity relationshipsByName][@"basicModel"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:basicModelRelationship attributes:@{@"basicModel": @"uniqueID"}];
[userEntityMapping addConnection:connection];
In core data a RegisteredUser
has a one to one relationship with a BasicModel
entity called basicModel
. Additionally the BasicModel
entity has a uniqueID attribute.
As far as I can figure, I am creating the RKConnectionDescription correctly according to the example here. The problem is that when I call initWithRelationship:attributes:
, it asserts that there is a basicModel
attribute on the RegisteredUser
entity, which of course the isn't. There is only a basicModel
relationship and so I get the crash I described above.
Why does RestKit even check that the RegisteredUser
has an attribute basicModel
if it expects me to be creating a connection for a relationship? Am I missing something here?
Upvotes: 0
Views: 238
Reputation: 119031
Using a RKConnectionDescription
is correct, but how you're using it is wrong. In this line:
initWithRelationship:basicModelRelationship attributes:@{@"basicModel": @"uniqueID"}];
you are telling RestKit about the relationship, but that it should use an attribute named basicModel
in the RegisteredUser
entity to find the BasicModel
to connect to, matching the value of basicModel
in the RegisteredUser
with uniqueID
in the BasicModel
.
So, it is basicModel
that needs to change. You need to add an attribute, like basicModelId
to the RegisteredUser
entity. It can be transient. Add it to the RegisteredUser
mapping and use basicModelId
in the RKConnectionDescription
.
Upvotes: 1