Reputation: 739
My goal is to implement one-to-many and many-to-one relationship connection with RestKit. I'm using version 0.20pre6. This page http://restkit.org/api/0.20.0/Classes/RKConnectionDescription.html#overview reports half example. First example is many-to-one. json:
{ "project":
{ "id": 12345,
"name": "My Project",
"userID": 1
}
}
code:
NSEntityDescription *projectEntity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:managedObjectContext];
NSRelationshipDescription *userRelationship = [projectEntity relationshipsByName][@"user"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:userRelationship attributes:@{ @"userID": @"userID" }];
The thing i missed during my first attempt is that userID needs to be in the Entity too. Otherwise it won't work. I don't really understand why... anyway it works.
My problem is related to the second example which is a one-to-many. Json example:
{ "project":
{ "id": 12345,
"name": "My Project",
"userID": 1,
"teamMemberIDs": [1, 2, 3, 4]
}
}
code:
NSEntityDescription *projectEntity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:managedObjectContext];
NSRelationshipDescription *teamMembers = [projectEntity relationshipsByName][@"teamMembers"]; // To many relationship for the `User` entity
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:teamMembers attributes:@{ @"teamMemberIDs": @"userID" }];
Now... teamMemberIDs needs to be in the Entity definition just like userID in the previous example. Here are my questions:
Upvotes: 0
Views: 933
Reputation: 78
I was struggling with this exact same problem, but was eventually able to find solution. Hopefully this will help you out.
Using the example:
Upvotes: 4