我有一个 iOS 应用程序使用 restKit 从服务器中提取数据并将其保存到核心数据中。
背景资料
来自服务器的响应具有嵌套对象。见下文。
<node>
<entryDate>2013-08-31T20:08:53-06:00</entryDate>
<followId>0</followId>
<ipAddress>255.255.255.0</ipAddress>
<likeStatus>-3</likeStatus>
<nodeId>20</nodeId>
<personId>2</personId>
<point>
<city>Bluffdale</city>
<copy>An amazing discovery was unearthed as a local farmer was plowing his field</copy>
<entryDate>2013-08-31T20:08:53-06:00</entryDate>
<ipAddress>255.255.255.0</ipAddress>
//..... removed some to be concise
<trend>0.0</trend>
</point>
<point>
//there are many points per node all which contain fields like above
</point>
</node>
我创建了两个实体。一个用于节点级数据,一个用于点级数据。我在两者之间有一对多的关系,我正在像这样映射结果。
RKEntityMapping *nodeMapping = [RKEntityMapping mappingForEntityForName:@"NodeLevel" inManagedObjectStore:managedObjectStore];
[nodeMapping addAttributeMappingsFromDictionary:@{
@"followId.text": @"followId",
@"likeStatus.text": @"likeStatus",
@"nodeId.text": @"nodeId",
@"personId.text": @"personId",
@"entryDate.text": @"entryDate"}];
nodeMapping.identificationAttributes = @[ @"nodeId" ];
RKEntityMapping *pointMapping = [RKEntityMapping mappingForEntityForName:@"PointLevel" inManagedObjectStore:managedObjectStore];
[pointMapping addAttributeMappingsFromDictionary:@{
@"nodeId.text": @"nodeId",
@"copy.text": @"paragraph",
//....removed some to be concise
@"trend.text": @"trend"}];
pointMapping.identificationAttributes = @[ @"pointId" ];
[nodeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"point" toKeyPath:@"node" withMapping:pointMapping]];
目前节点级数据映射到节点实体,点级数据映射到点实体。这是我遇到麻烦的地方。
问题
任何给定节点中都有很多点。他们每个人都有一个<copy>
我需要用来填充 UITextView 的字段。这是我一直在使用非嵌套响应的代码,它运行良好。
[[RKObjectManager sharedManager] getObjectsAtPath:getArticle parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
self.htmlString = [[mappingResult.firstObject valueForKey:@"paragraph"]description];
if (self.htmlString != nil) {
NSDictionary *options = @{DTUseiOS6Attributes: [NSNumber numberWithBool:YES]};
NSData *htmlData = [self.htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *stringArticle = [[NSAttributedString alloc] initWithHTMLData:htmlData options:options documentAttributes:NULL];
self.newsDetailText.attributedText = stringArticle;
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
但是现在我正在映射一个嵌套响应,抛出一个异常,因为the entity NodeLevel is not key value coding-compliant for the key "paragraph"
使用 RKMappingResult 非常方便,但我不确定如何指定要从中提取的实体。
谁能帮我把这个拼出来!