RestKit 0.23.1一对多关系映射不起作用

时间:2014-06-19 03:00:24

标签: json core-data relationship fault

过去几天,我一直在努力弄清楚如何使用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;
})

注意&#34;放置&#34;和&#34;用户&#34;显示好吗?这些是1对1的对象映射,工作正常。当我直接在NSLog语句中调用它们时,是否不会出现链接?

最终编辑,根据Wain的建议,我从关系数组中取出第一个对象,然后返回相关的网络链接。

if ([postObject.links count] > 0) {
    RKWebLink *link = [[postObject.links allObjects] objectAtIndex:0];
    NSLog(@"Link title: %@", link.title);
}

1 个答案:

答案 0 :(得分:0)

links = "<relationship fault: 0x114a05820 'links'>";

这并不意味着关系内容不存在,只是意味着它尚未加载到内存中(这是一个错误)。当您尝试使用关系内容时,它将被加载。

您的代码看起来很好。方括号只是意味着你有一个对象的数组/列表而不是一个并且不是问题(这是一件好事)。