【发布时间】:2014-06-19 03:00:23
【问题描述】:
在过去的几天里,我一直在努力弄清楚如何使用 RestKit 0.23.1 将此 JSON 数据映射到 Core Data 中的托管对象。我的数据包含多个“对象”,每个对象都有自己的一个或多个“链接”列表,这些“链接”是包含 URL 预览信息的结构化对象:
objects": [
{
"id": "03fbbec709d261c87a64d979fe1e530b",
"object_type": "note",
"links": [
{
"id": "d48066cf6daa007e31f8915831f46fc2",
"urlSite": "techcrunch.com",
"siteName": null,
"urlString": "http://techcrunch.com/2014/06/16/secret-finally-lets-all-your-freaky-friends-have-their-own-feed/",
"urlShortened": "http://gcq.me/1vzsVJY",
"title": "Secret Finally Lets All Your Freaky Friends Have Their Own Feeds | TechCrunch",
"previewImage": "http://tctechcrunch2011.files.wordpress.com/2014/06/secret2.jpg?w=650",
"previewText": "Secret Finally Lets All Your Freaky Friends Have Their Own Feeds | TechCrunch",
"lastUpdated": "2014-06-17 06:50:56",
"lastVisited": "2014-06-17 06:50:56",
"visitCount": 0
}
]
}]
我创建了函数来帮助建立我的映射关系:
+ (RKEntityMapping*)webLinkMapping {
RKManagedObjectStore* managedObjectStore = [RKManagedObjectStore defaultStore];
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"WebLink" inManagedObjectStore:managedObjectStore];
mapping.identificationAttributes = @[ @"objectId" ];
[mapping addAttributeMappingsFromDictionary:@{@"id": @"objectId"}];
[mapping addAttributeMappingsFromArray:@[ @"urlSite", @"siteName", @"urlString", @"urlShortened",
@"title", @"previewImage", @"previewText", @"lastUpdated",
@"lastVisited", @"visitCount"]];
return mapping;
}
+ (RKEntityMapping*)streamObjectMapping {
RKManagedObjectStore* managedObjectStore = [RKManagedObjectStore defaultStore];
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"StreamObject" inManagedObjectStore:managedObjectStore];
mapping.identificationAttributes = @[ @"objectId" ];
[mapping addAttributeMappingsFromDictionary:@{
@"id": @"objectId",
}];
[mapping addAttributeMappingsFromArray:@[ @"object_type"]];
[mapping addRelationshipMappingWithSourceKeyPath:@"links" mapping:[self webLinkMapping]];
//[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"links" toKeyPath:@"links" withMapping:[self webLinkMapping]]];
return mapping;
}
我几乎可以肯定是在 JSON 中返回的链接数组周围的一对方括号导致我的映射无法正常工作。我在返回的流对象中不断出现关系错误:
links = "<relationship fault: 0x114a05820 'links'>";
使用 sqlite 浏览器查看实际数据表,我看到我的链接对象与父对象中正确的关联 objectId 一起存储,因此当我请求我的流对象时它们没有正确返回.关于我如何纠正这个问题的任何想法?
关于加载到内存中,它似乎没有这样做:
RKStreamObject* postObject = [_postObjects objectAtIndex:indexPath.row];
NSLog(@"Links: %@", postObject.links);
2014-06-20 11:25:11.249 MyApp[6026:151920] Links: Relationship 'links' fault on managed object (0x110c47ee0) <RKStreamObject: 0x110c47ee0> (entity: StreamObject; id: 0xd000000001000000 <x-coredata://F96BC470-82A5-4C56-81A9-71C6129F967B/StreamObject/p64> ; data: {
body = "http://techcrunch.com/2014/06/16/secret-finally-lets-all-your-freaky-friends-have-their-own-feed/";
"comment_count" = 0;
controllerSource = nil;
"gc_updated" = "2014-06-20 16:25:10 +0000";
"liked_by_me" = 0;
likes = 0;
links = "<relationship fault: 0x1141bce40 'links'>";
objectId = 03fbbec709d261c87a64d979fe1e530b;
"object_type" = note;
photo = nil;
"photo_feed" = nil;
"photo_height" = 0;
"photo_width" = 0;
place = "0xd000000000100002 <x-coredata://F96BC470-82A5-4C56-81A9-71C6129F967B/Place/p4>";
privacy = F;
"subscribed_by_me" = 1;
timestamp = "2014-06-17T06:51:00-05:00";
"timestamp_as_words" = "3 days ago";
"timestamp_formatted" = "Tuesday, June 17, 2014 at 06:51:00 CDT";
"timestamp_unix" = 1403005860;
user = "0xd0000000000c0004 <x-coredata://F96BC470-82A5-4C56-81A9-71C6129F967B/User/p3>";
videoId = nil;
videoSource = nil;
})
注意“地点”和“用户”的显示方式好吗?这些是工作正常的一对一对象映射。当我在 NSLog 语句中直接调用它们时,不应该出现链接吗?
最终编辑,根据 Wain 的建议,我从关系数组中提取了第一个对象,它返回了相关的 Web 链接。
if ([postObject.links count] > 0) {
RKWebLink *link = [[postObject.links allObjects] objectAtIndex:0];
NSLog(@"Link title: %@", link.title);
}
【问题讨论】:
标签: json core-data relationship fault