Restkit嵌套对象映射问题

时间:2014-07-15 12:14:05

标签: ios restkit

我正在尝试使用以下json结构填充两个核心数据实体。

{
  "fldrs": [
    {
      "FldName": "Messages",
      "FldID": "Messages",
      "PrntFldID": null,
      "UnRdCunt": "0",
      "Cunt": "0",
      "IsDflt": "True",
      "FldIconID": null,
      "SubFldrs": [
        {
          "FldName": "Compose",
          "FldID": "INDV9",
          "PrntFldID": "Messages",
          "UnRdCunt": "0",
          "Cunt": "0",
          "IsDflt": "False",
          "FldIconID": null,
          "SubFldrs": null,
          "tasksLists": null
        },
        {
          "FldName": "Inbox",
          "FldID": "INDV10",
          "PrntFldID": "Messages",
          "UnRdCunt": "2",
          "Cunt": "12",
          "IsDflt": "True",
          "FldIconID": null,
          "SubFldrs": null,
          "tasksLists": null
        }
      ],
      "tasksLists": null
    },
    {
      "FldName": "My Tasks",
      "FldID": "My Tasks",
      "PrntFldID": null,
      "UnRdCunt": "0",
      "Cunt": "0",
      "IsDflt": "False",
      "FldIconID": null,
      "SubFldrs": [
        {
          "FldName": "Pending",
          "FldID": "INDV1",
          "PrntFldID": "My Tasks",
          "UnRdCunt": "9",
          "Cunt": "9",
          "IsDflt": "False",
          "FldIconID": null,
          "SubFldrs": null,
          "tasksLists": null
        }
      ],
      "tasksLists": null
    }
  ]
}

这里' SubFldrs'是' flds'的儿童实体。

这是我正在使用的数据模型

enter image description here

以下是使用

的模型类
@class ParentFolder;

@interface SubFolder : NSManagedObject

@property (nonatomic, retain) NSString * fldId;
@property (nonatomic, retain) NSString * fldName;
@property (nonatomic, retain) NSString * isDefault;
@property (nonatomic, retain) NSString * msgCount;
@property (nonatomic, retain) NSString * prntFolderId;
@property (nonatomic, retain) NSString * unreadCount;
@property (nonatomic, retain) ParentFolder *subFolder_folder;

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class SubFolder;

@interface ParentFolder : NSManagedObject

@property (nonatomic, retain) NSString * fldId;
@property (nonatomic, retain) NSString * fldName;
@property (nonatomic, retain) NSSet *folder_SubFolder;
@end

@interface ParentFolder (CoreDataGeneratedAccessors)

- (void)addFolder_SubFolderObject:(SubFolder *)value;
- (void)removeFolder_SubFolderObject:(SubFolder *)value;
- (void)addFolder_SubFolder:(NSSet *)values;
- (void)removeFolder_SubFolder:(NSSet *)values;

@end

Restkit Mapping通过以下代码完成

RKObjectMapping* subFolderMapping = [RKObjectMapping mappingForClass:[SubFolder class] ];
    [subFolderMapping addAttributeMappingsFromDictionary:@{
                                                           @"FldName":   @"fldName",
                                                           @"FldID":   @"fldId",
                                                           @"IsDflt":   @"isDefault",
                                                           @"UnRdCunt":   @"unreadCount",
                                                           @"Cunt":   @"msgCount",
                                                           @"PrntFldID":   @"prntFolderId"
                                                           }];

    RKObjectMapping* folderMapping = [RKObjectMapping mappingForClass:[ParentFolder class] ];
    [folderMapping addAttributeMappingsFromDictionary:@{
                                                        @"FldID":  @"fldId",
                                                        @"FldName": @"fldName"
                                                        }];

    [folderMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"subFolder"
                                                                                   toKeyPath:@"SubFldrs"
                                                                                 withMapping:subFolderMapping]];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:folderMapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:nil
                                                                                           keyPath:@"fldrs"
                                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    return responseDescriptor;

父实体(ParentFolder)根据需要填充,但子实体(SubFolder)始终为空。 有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

在创建关系映射时,您似乎只是按错了顺序:

[folderMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"SubFldrs"
                                                                              toKeyPath:@"subFolder"
                                                                            withMapping:subFolderMapping]];
相关问题