2014-05-15 35 views
2

我有一个关于在CoreDataRestKit之间使用逻辑的简单问题。CoreData和RestKit

我正在使用RestKit将我的JSON响应映射到CoreData实体。我有一个评论事件。 我正在发送一个请求以获取有关事件的信息,另外还有一个(现在)的评论。

有没有办法映射独立于我以前的事件的评论,并加入他们之后还是必须加入他们与事件时有映射?我不知道最好的办法是什么。

在我未来的实现中,我想发送以获取事件及其注释的信息。但我仍然希望继续使用我的第二种方法来获取评论,而不必收到整个活动。

"comments": [ 
    { 
     "id": 23, 
     "user_id": 9, 
     "commentable_id": 12, 
     "commentable_type": "Event", 
     "content": "This is the content of the event", 
     "created_at": "2013-04-19 19:28:42.533901", 
     "updated_at": "2013-04-19 19:28:42.533901" 
    } 
] 
+0

你看过RKConnectionDescription。它允许您将注释独立映射到父模型。你的JSON看起来如何。如果你寄给我样品,我可能会向你提供答案。 – Sandeep

+0

我添加了一个典型的'JSON'响应。如果我将这些注释独立映射到父项,那么在映射之后如何将它们加入到父项中?无论如何,谢谢你这么快的回答。 – BriceB

回答

0

您可以使用外键映射(RKConnectionDescription,根据@ insane-36的注释)获得RestKit连接对象之间的关系。如果适当的目标对象在上下文中可用,那么将建立连接,如果不是,则不做任何事情。

如果您不想这样做,那么您需要编写代码来复制该任务,即获取注释,遍历它们,获取关联的事件,连接关系,保存。

让RestKit连接关系对您而言肯定更容易,并且并不妨碍您在没有应该附加事件的情况下请求注释。

请注意,您可以在事件和注释映射上指定外键映射,以便首先加载哪个对象。当处理后者映射时,RestKit将连接到另一个映射。

+0

附加信息:http://stackoverflow.com/questions/17326087/foreign-key-relationship-mapping-with-restkit – Wain

+0

我知道我可以使用外键映射,但是可以手动添加关系到现有的对象?我有类似的是我的:'event.comments = comments_array'其中'comments_array'是我的'JSON'请求和'事件'一个现有对象的新映射? 基本上我不想重新加载整个事件,当我重新加载评论的事件。 – BriceB

+0

关闭。您的托管对象子类应该有一些为关系添加的“NSSet”方法。你可以从你从映射结果得到的NSArray创建一个'NSSet',并用它来填充关系。如果您不使用外键映射,则只需要此操作。 – Wain

0

假设你有JSON像上面,你将不得不增加一个属性对应一个特定的评论属于这样的,我们的JSON看起来像哪个事件,

"comments": [ 
    { 
     "id": 23, 
     "user_id": 9, 
     "commentable_id": 12, 
     "commentable_type": "Event", 
     "content": "This is the content of the event", 
     "created_at": "2013-04-19 19:28:42.533901", 
     "updated_at": "2013-04-19 19:28:42.533901", 
     "event_id": "10", /* Note this is a new attribute to map to the parent entity */ 
    } 
] 

现在,添加一个新的属性给你的评论模型,这样这个event_id将被存储。我们创建一个属性eventId

所以,让我们创建映射为实体,

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Comment" inManagedObjectStore:[RKManagedObjectStore defaultStore]]; 
[mapping setIdentificationAttributes:@[@"identifier"]]; 
[mapping addAttributeMappingsFromDictionary:@{ 
    @"id": @"identifier", 
    @"updated_at": @"updatedAt", 
    @"created_at": @"createdAt", 
    @"user_id": @"userId", 
    @"commentable_id": @"commentableId", 
    @"commentable_type": @"commentableType", 
    @"content": @"content", 
    @"event_id": @"eventId" 
    }]; 

然后,我们将不得不增加连接映射事项标识纠正事件,

NSRelationshipDescription *eventRelationship = [[mapping entity] relationshipsByName][@"event"]; 

[mapping addConnection:[[RKConnectionDescription alloc] initWithRelationship:eventRelationship attributes:@{@"eventId": @"identifier"}]]; // this line says that you have to have eventId in your comments entity and then the identifier in event entity to which the eventId will be mapped to. 

就是这样,你可以现在创建RKResponseDescriptor和RKManagedObjectRequestOperation来提取评论,它将指向正确的事件。

+0

谢谢你的回答。这是很好的映射。但我的主要问题是如何将它链接到现有的对象。我同意这种映射是必需的。 – BriceB

+0

一旦您使用此新连接描述再次获取所有评论,它就会与现有事件相关联。 – Sandeep