簡體   English   中英

RKEntityMapping與數組

[英]RKEntityMapping with array

嘗試映射此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都必須包含多個Parents,但是在創建Parent實體RestKit時,請將其與parentID統一,並用new替換現有的。 是否可以為每個GroupValue保留Parent實體? 有沒有不統一實體的方法? 不設置identificationAttributes沒有幫助

您無法直接從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" }];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM