0

尝试映射这个 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 集始终为空...

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

已编辑

以下是日志:

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 必须包含多个 Parent,但是在创建 Parent 实体时 RestKit 将其与 parentID 统一并用新的替换现有的。是否可以为每个 GroupValue 保留父实体?有什么方法不统一实体吗?不设置 IdentificationAttributes 没有帮助

4

1 回答 1

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" }];
于 2013-09-06T13:50:11.350 回答