2014-10-02 85 views
0

我正在使用RestKit與核心數據,並且無法映射實體之間的關係。問題是我有3個不同的jsons(3個不同的屏幕),這些jsons有相互關聯的數據。我的3個實體是發佈,對話和消息。關係如此:每篇文章都有很多對話(對多關係),每個對話都有很多信息(對多關係)。與關係的RestKit問題

這裏是樁的陣列的第一JSON:

{"success":true,"result": 
[{"totalUsers":1,"lastMessageDate":1411612821000,"id":874,"title":"My post title"}, 
{"totalUsers":3,"lastMessageDate":1411536669000,"id":539,"title":"Message me"}]} 

這裏是第二JSON,這是對話的特定交的數組:

{"success":true,"result": 
[{"badgeSize":1,"lastMessage": 
{"id":1725,"text":"hey","datePublished":141153372347},"id":208,"username":"energydrink"}]} 

這裏是第三json進行特定會話:

{"success":true,"result": 
{"spamStatus":false,"messages": 
[{"id":416,"text":"hello","datePublished":1403432789000}, 
{"id":380,"text":"whats up","datePublished":1403432144221}]}} 

因此,您可以看到,關係的數據是分成3個jsons。下面是我所目前RestKit中配置我的人際關係和響應描述:

[conversationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"messages" toKeyPath:@"messages" withMapping:messageMapping]]; 

[postMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"conversations" toKeyPath:@"conversations" withMapping:messageMapping]]; 

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:postMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"result" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

因爲響應描述符與postMapping映射,我無法正確地映射爲第2和數據3 jsons然後核心數據中存儲與我的配置。任何幫助是極大的讚賞。

回答

0

我想通了。基本上,我使用RestKit的映射爲第一個JSON。當在第一個視圖控制器上調用tableView:didSelectRowAtIndexPath:時,我將post對象傳遞給第二個視圖控制器。對於第二個json響應,我將數據反序列化並將對話添加到該後置對象。然後,我簡單地稱爲[[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext saveToPersistentStore:&error];。現在它工作得很好。