Ios Restkit在嵌套外键关系中使用@parent
我最难使用RestKit建立一些关系 从Ios Restkit在嵌套外键关系中使用@parent,ios,json,restkit,restkit-0.20,Ios,Json,Restkit,Restkit 0.20,我最难使用RestKit建立一些关系 从项目到场馆,存在一对多的关系,但通过多个层次的关系 以下是JSON结构的概要: { "id": 1, "name": "Venue Name", "sections": [ { "id": 1, "name": "Paper", "categories": [ { "
项目
到场馆
,存在一对多的关系,但通过多个层次的关系
以下是JSON结构的概要:
{
"id": 1,
"name": "Venue Name",
"sections": [
{
"id": 1,
"name": "Paper",
"categories": [
{
"id": 1,
"name": "Paper Goods",
"items": [
{
"venue" : 1
// Other data goes here
}
}
}
}
我正在尝试将场馆
外键连接到场馆
的id
属性
我尝试了以下几点:
NSEntityDescription * itemEntity = [NSEntityDescription entityForName:@"DSItem" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
NSRelationshipDescription * venueRel = [itemEntity relationshipsByName][@"venue"];
RKConnectionDescription * connection = [[RKConnectionDescription alloc] initWithRelationship:venueRel attributes:@{@"venue" : @"pk"}]; // id --> pk on DSVenue class
但在对象映射后,场地属性可能为零
如何在此中使用@parent
创建外键关系?还是有更好的办法
比如:
@{@"@parent.@parent.@parent.pk" : @"venue"}
我已经看到了这个问题,但它似乎适用于将属性链接到父属性,而不是将外键链接到实体
任何帮助都将不胜感激
编辑:以下是所有映射代码
RKEntityMapping *itemMapping = [RKEntityMapping mappingForEntityForName:@"DSItem" inManagedObjectStore:managedObjectStore];
itemMapping.identificationAttributes = [DSItem identificationAttributes];
[itemMapping addAttributeMappingsFromDictionary:[DSItem attributeMappings]];
NSEntityDescription * itemEntity = [NSEntityDescription entityForName:@"DSItem" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
NSRelationshipDescription * venueRel = [itemEntity relationshipsByName][@"venue"];
RKConnectionDescription * connection = [[RKConnectionDescription alloc] initWithRelationship:venueRel keyPath:@"venue"];
[itemMapping addConnection:connection];
RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"DSCategory" inManagedObjectStore:managedObjectStore];
categoryMapping.identificationAttributes = [DSCategory identificationAttributes];
[categoryMapping addAttributeMappingsFromDictionary:[DSCategory attributeMappings]];
[categoryMapping addRelationshipMappingWithSourceKeyPath:@"items" mapping:itemMapping];
RKEntityMapping *sectionMapping = [RKEntityMapping mappingForEntityForName:@"DSSection" inManagedObjectStore:managedObjectStore];
sectionMapping.identificationAttributes = [DSSection identificationAttributes];
[sectionMapping addAttributeMappingsFromDictionary:[DSSection attributeMappings]];
[sectionMapping addRelationshipMappingWithSourceKeyPath:@"categories" mapping:categoryMapping];
RKEntityMapping *venueMapping = [RKEntityMapping mappingForEntityForName:@"DSVenue" inManagedObjectStore:managedObjectStore];
[venueMapping addAttributeMappingsFromDictionary:[DSVenue attributeMappings]];
venueMapping.identificationAttributes = [DSVenue identificationAttributes];
[venueMapping addRelationshipMappingWithSourceKeyPath:@"sections" mapping:sectionMapping];
RKResponseDescriptor *venueDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:venueMapping
method:RKRequestMethodGET
pathPattern:[DSVenue pathPattern]
keyPath:[DSVenue keyPath]
statusCodes:successIndexSet];
RKResponseDescriptor *itemDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:itemMapping
method:RKRequestMethodGET
pathPattern:[DSItem pathPattern]
keyPath:[DSItem keyPath]
statusCodes:successIndexSet];
[manager addResponseDescriptorsFromArray:@[itemDescriptor, venueDescriptor]];
不要做你想做的事。您的映射看起来很好,并且假设您在模型中配置了反向关系,您只需从项目实体导航关系即可到达关联的场馆实体
也就是说,我认为
@parent.id
引用应该能够使连接成为可能(通过提供对id的访问)。但再一次,真的不需要。您可以在模型上获取和断言,以获取您所拥有的关系所需的任何信息。您的JSON是嵌套的,那么您为什么要尝试使用外键映射呢?你应该直接将映射与关系联系起来。基本上,我需要能够按地点对项目进行排序。如何将一个3级深度的关系链接到最顶层的实体?您没有将所有关系链接到顶层映射,它们是在每个级别创建的。显示要映射到的模型中的实体。我添加了映射的所有代码。我试图避免在谓词中跨越多个关系可能出现的性能问题,但我认为你是对的。我会那样做的。