我一直在努力研究如何使用RestKit 0.23.1過去幾天將此JSON數據映射到Core Data中的管理對象。我的數據包含多個「對象」,每個都有自己的一個或多個「鏈接」,這是包含預覽信息的URL結構化對象列表:RestKit 0.23.1一對多關係映射不起作用
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'>";
使用源碼的瀏覽器,我看到我的鏈接對象存儲與父對象的正確關聯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;
})
的通知「的地方」和「用戶」是如何出現好嗎?這些是1對1的對象映射,可以很好地工作。當我直接在NSLog語句中調用它們時,不應該出現鏈接?
根據Wain的建議進行最終編輯時,我從關係數組中拉出第一個對象,並返回相關的Web鏈接。
if ([postObject.links count] > 0) {
RKWebLink *link = [[postObject.links allObjects] objectAtIndex:0];
NSLog(@"Link title: %@", link.title);
}
基於其他類似的線程和相同的迴應,我傾向於同意你的看法,除了當我訪問鏈接變量時,我仍然會遇到同樣的錯誤。我在帖子中添加了更多信息,以顯示我如何嘗試訪問鏈接。 – webguyatwork
從關係中提取第一個項目並記錄下來。謹慎地點燃故障,儘可能不會觸發並加載數據。 – Wain
你,先生,是救命恩人!我從來沒有(也許最終)會想到試試這個。這正是我需要的。謝謝!! – webguyatwork