Restkit嵌套对象映射问题
Posted
技术标签:
【中文标题】Restkit嵌套对象映射问题【英文标题】:Restkit nested Object mapping issue 【发布时间】:2014-07-15 12:14:05 【问题描述】:我正在尝试使用以下 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”的子实体。
这是我正在使用的数据模型
以下是使用的模型类
@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】:在您创建关系映射时,您的键似乎顺序错误:
[folderMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"SubFldrs"
toKeyPath:@"subFolder"
withMapping:subFolderMapping]];
【讨论】:
子表填充了空值并且只有一条记录可用。请帮帮我。 刚刚注意到您展示了一个核心数据模型并讨论了表,但您使用了NSObject
子类和对象映射。您是否使用 Core Data?
抱歉,实际上这些只是 NSManagedObject 的子类。我将编辑我的问题。现在我可以根据您的建议正确保存数据。非常感谢:-)
如果您也可以更正问题中的映射(针对实体而不是对象)以帮助将来查看此问题的人,那就太好了。以上是关于Restkit嵌套对象映射问题的主要内容,如果未能解决你的问题,请参考以下文章