尝试映射这个json
{
"result": [
{
"group_id": 3,
"name": "_8",
"parent_id": [
1,
4039
],
"position": 6,
"title": "/8",
"id": 18624
},
{
"group_id": 3,
"name": "02",
"parent_id": [
1,
3118
],
"position": 13,
"title": "02",
"id": 18551
}
],
"metadata": {
"resultset": {
"count": 2373,
"limit": 100,
"offset": 0
}
}
}
我使用下一个映射:
RKEntityMapping *groupMapping = [RKEntityMapping mappingForEntityForName:@"Group" inManagedObjectStore:managedObjectStore];
groupMapping.identificationAttributes = @[ @"groupID" ];
// If source and destination key path are the same, we can simply add a string to the array
[groupMapping addAttributeMappingsFromArray:@[ @"name", @"title" ]];
[groupMapping addAttributeMappingsFromDictionary:@{
@"id": @"groupID"
}];
RKEntityMapping *groupValuesMapping = [RKEntityMapping mappingForEntityForName:@"GroupValue" inManagedObjectStore:managedObjectStore];
groupValuesMapping.identificationAttributes = @[ @"valueID", @"groupID" ];
[groupValuesMapping addAttributeMappingsFromArray:@[ @"name", @"title", @"position", @"popular" ]];
[groupValuesMapping addAttributeMappingsFromDictionary:@{
@"id": @"valueID",
@"group_id": @"groupID"
}];
RKEntityMapping *parentsMapping = [RKEntityMapping mappingForEntityForName:@"Parent" inManagedObjectStore:managedObjectStore];
parentsMapping.identificationAttributes = @[ @"parentID" ];
[parentsMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"parentID"]];
RKRelationshipMapping *parentsRelationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"parent_id"
toKeyPath:@"parents"
withMapping:parentsMapping];
parentsRelationshipMapping.assignmentPolicy = RKReplaceAssignmentPolicy;
[groupValuesMapping addPropertyMapping:parentsRelationshipMapping];
RKObjectMapping *metadataMapping = [RKObjectMapping mappingForClass:[Metadata class]];
[metadataMapping addAttributeMappingsFromArray:@[ @"count", @"limit", @"offset" ]];
RKObjectMapping *containerMapping = [RKObjectMapping mappingForClass:[GroupValueContainer class]];
[containerMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"metadata.resultset"
toKeyPath:@"metadata"
withMapping:metadataMapping]];
[containerMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"result"
toKeyPath:@"groupValues"
withMapping:groupValuesMapping]];
除了parent_id结构之外,一切都是好的。有谁知道,我忘记了什么? parentID set始终为空...
GroupValue.h是
@class Group, Parent;
@interface GroupValue : NSManagedObject
@property (nonatomic, retain) NSNumber * groupID;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * position;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSNumber * valueID;
@property (nonatomic, retain) NSNumber * popular;
@property (nonatomic, retain) Group *group;
@property (nonatomic, retain) NSSet *parents;
@end
@interface GroupValue (CoreDataGeneratedAccessors)
- (void)addParentsObject:(Parent *)value;
- (void)removeParentsObject:(Parent *)value;
- (void)addParents:(NSSet *)values;
- (void)removeParents:(NSSet *)values;
@end
GroupValueContainer.h是
@interface GroupValueContainer : NSObject
@property (nonatomic, strong) NSArray *groupValues;
@property (nonatomic, strong) Metadata *metadata;
@end
Parent.h是
@class GroupValue;
@interface Parent : NSManagedObject
@property (nonatomic, retain) NSNumber * parentID;
@property (nonatomic, retain) GroupValue *groupValue;
@end
EDITED
以下是日志:
Performing nested object mapping using mapping <RKRelationshipMapping: 0x1f5cb240 result => groupValues> for data: {
"group_id" = 3;
id = 18624;
name = "_8";
"parent_id" = (
1,
4039
);
position = 6;
title = "/8";
}
Mapping attribute value keyPath 'name' to 'name'
Skipped mapping of attribute value from keyPath 'name to keyPath 'name' -- value is unchanged (_8)
Mapping attribute value keyPath 'title' to 'title'
Skipped mapping of attribute value from keyPath 'title to keyPath 'title' -- value is unchanged (/8)
Mapping attribute value keyPath 'position' to 'position'
Skipped mapping of attribute value from keyPath 'position to keyPath 'position' -- value is unchanged (6)
Did not find mappable attribute value keyPath 'popular'
Mapping attribute value keyPath 'id' to 'valueID'
Skipped mapping of attribute value from keyPath 'id to keyPath 'valueID' -- value is unchanged (18624)
Mapping attribute value keyPath 'group_id' to 'groupID'
Skipped mapping of attribute value from keyPath 'group_id to keyPath 'groupID' -- value is unchanged (3)
Mapping one to many relationship value at keyPath 'parent_id' to 'parents'
Performing nested object mapping using mapping <RKRelationshipMapping: 0x1f5cb070 parent_id => parents> for data: 1
Starting mapping operation...
Performing mapping operation: <RKMappingOperation 0x1f50e060> for 'Parent' object. Mapping values from object 1 to object <Parent: 0x2018add0> (entity: Parent; id: 0x20159ec0 <x-coredata://DC16066E-D7ED-4AB4-BC94-36528E3818EC/Parent/p2> ; data: {
groupValue = nil;
parentID = 1;
}) with object mapping (null)
Mapping attribute value keyPath '(null)' to 'parentID'
Skipped mapping of attribute value from keyPath '(null) to keyPath 'parentID' -- value is unchanged (1)
Finished mapping operation successfully...
Performing nested object mapping using mapping <RKRelationshipMapping: 0x1f5cb070 parent_id => parents> for data: 4039
Starting mapping operation...
Performing mapping operation: <RKMappingOperation 0x1f50e060> for 'Parent' object. Mapping values from object 4039 to object <Parent: 0x1f50cac0> (entity: Parent; id: 0x2015a300 <x-coredata://DC16066E-D7ED-4AB4-BC94-36528E3818EC/Parent/p48> ; data: {
groupValue = nil;
parentID = 4039;
}) with object mapping (null)
Mapping attribute value keyPath '(null)' to 'parentID'
Skipped mapping of attribute value from keyPath '(null) to keyPath 'parentID' -- value is unchanged (4039)
Finished mapping operation successfully...
Found transformable value at keyPath 'parent_id'. Transforming from type '__NSArrayM' to 'NSSet'
Mapping a to-many relationship for an `NSManagedObject`. About to apply value via mutable[Set|Array]ValueForKey
Mapped `NSSet` relationship object from keyPath 'parent_id' to 'parents'. Value: {(
<Parent: 0x2018add0> (entity: Parent; id: 0x20159ec0 <x-coredata://DC16066E-D7ED-4AB4-BC94-36528E3818EC/Parent/p2> ; data: {
groupValue = nil;
parentID = 1;
}),
<Parent: 0x1f50cac0> (entity: Parent; id: 0x2015a300 <x-coredata://DC16066E-D7ED-4AB4-BC94-36528E3818EC/Parent/p48> ; data: {
groupValue = nil;
parentID = 4039;
})
)}
Finished mapping operation successfully...
一切似乎都没问题,父母得救,但连接断了
已编辑2
发现有什么问题。每个GroupValue必须包含乘法父项,但在创建父实体时,RestKit将其与parentID统一并替换为new。是否可以为每个GroupValue保留Parent实体?是不是统一实体的方法?没有设置identificationAttributes没有帮助
答案 0 :(得分:1)
您无法直接映射JSON中的ID数组来连接关系。关系连接需要作为外键映射完成。这意味着parent_id
的内容需要映射到类(parentIds
)上的(临时)属性,然后关系映射通过RKConnectionDescription
使用该属性。类似的东西:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"GroupValue" inManagedObjectContext:managedObjectContext];
NSRelationshipDescription *parents = [entity relationshipsByName][@"parents"]; // To many relationship
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:parents attributes:@{ @"parentIds": @"parentID" }];