Restkit将响应映射到父实体 - 当响应没有父实体值

时间:2016-06-21 10:00:21

标签: ios json core-data restkit restkit-0.20

我在我的项目中使用RestKit 0.2版本。我有一种情况需要将响应映射到已保存的父实体。

我的父母班级是对话&对话可以有很多消息。

首先,我将对话实体中的所有会话保存在Coredata中。

然后我调用此网址获取与特定会话相关的所有消息。

  

http://myUrl/conversations/bdc34e2a-fa1f-4dbe-83b4-3296ff657e93/messages

bdc34e2a-fa1f-4dbe-83b4-3296ff657e93是我的对话ID

这是我的json回复。

[  
 {  
  "id":"5f67f6f5-934d-403e-ac64-19dbd4defeda",
  "sent_at":"2016-06-08T12:24:28+0000",
  "read_at":null,
  "sender":{  },
  "content":"test message",
  "attachment":{  }
 },
 {  
  "id":"c4d18b33-4c54-4e7e-a964-a5cedc087122",
  "sent_at":"2016-06-08T09:59:41+0000",
  "read_at":null,
  "sender":{  },
  "content":"abc test message",
  "attachment":{  }
 },

我的Json响应没有会话属性,但我需要在相关会话下映射我的所有消息。

RKEntityMapping *messageMapping = [RKEntityMapping mappingForEntityForName:[Message entityName] inManagedObjectStore:[[DataStoreManager sharedManager] managedObjectStore]];

[messageMapping addAttributeMappingsFromDictionary:@{
                                                     @"id":@"identifier",
                                                     @"sent_at": @"date",
                                                     @"content": @"message",

                                                    }];

messageMapping.identificationAttributes = @[@"identifier"];

我对

的消息有多对一关系。

[messageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"conversation" withMapping:**conversationMapping**]];

但是我怎样才能创建conversationMapping?

  

已编辑 - 尝试使用路由&元数据

正如@Wain建议的那样,我正在尝试使用restkit路由

// conversation mapping
RKEntityMapping *conversationMapping = [RKEntityMapping mappingForEntityForName:[Conversation entityName] inManagedObjectStore:[[BADataStoreManager sharedManager] managedObjectStore]];

[conversationMapping addAttributeMappingsFromDictionary:@{
                                                          @"@metadata.routing.parameters.identifier": @"identifier"

                                                          }];


conversationMapping.identificationAttributes = @[@"identifier"];

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

这是我的回复描述符

RKResponseDescriptor *conversationDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[EntityMappingProvider messagesResponseMapping] method:RKRequestMethodAny pathPattern:@"/conversations/:identifier/messages" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[DataStoreManager sharedManager].router.routeSet addRoute:[RKRoute routeWithName:@"conversations" pathPattern:@"/conversations/:identifier/messages" method:RKRequestMethodGET]];

[[DataStoreManager sharedManager] addResponseDescriptor:conversationDescriptor];

[[DataStoreManager sharedManager] getObjectsAtPathForRouteNamed:@"conversations" object:conversation parameters:nil
                                                         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                             RKLogInfo(@"response %@",mappingResult);
                                                         }

                                                         failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                             RKLogInfo(@"error response %@", error.localizedDescription );
                                                         }];
用户从会话列表中选择一个会话时,将发送

对象:会话

但是仍然没有正确地将对话映射到消息。

&安培;我只能调用此方法一次,然后我收到以下错误。

  

原因:'无法添加与现有路线同名的路线。'

但它应该可用于所有对话,每当用户选择一个对话时,将调用此方法。

  

解决方案

我使用了响应描述符来映射对话&用户在对话中映射消息之前。因此,当我在对话中使用响应描述符时,它被映射到先前定义的描述符。

所以我不得不使用 pathPattern &指定响应描述符,然后正确映射,如@Wain解释的那样,我已经初始化了所有响应描述符一次&多次使用getObjectsAtPathForRouteNamed。

最后,这是我的对话映射&它的响应描述符

// conversation mapping
RKEntityMapping *conversationMapping = [RKEntityMapping mappingForEntityForName:[Conversation entityName] inManagedObjectStore:[[BADataStoreManager sharedManager] managedObjectStore]];
[conversationMapping addAttributeMappingsFromDictionary:@{
                                                          @"@metadata.routing.parameters.identifier": @"identifier"

                                                          }];
conversationMapping.identificationAttributes = @[@"identifier"];
[messageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"messages" withMapping:conversationMapping]];

// Response descriptor 
RKResponseDescriptor *conversationDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[EntityMappingProvider messagesResponseMapping] method:RKRequestMethodAny pathPattern:@"/conversations/:identifier/messages" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[DataStoreManager sharedManager].router.routeSet addRoute:[RKRoute routeWithName:@"conversations" pathPattern:@"/conversations/:identifier/messages" method:RKRequestMethodGET]];
[[DataStoreManager sharedManager] addResponseDescriptor:conversationDescriptor];
[[DataStoreManager sharedManager] getObjectsAtPathForRouteNamed:@"conversations" object:conversation parameters:nil
                                                         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                             RKLogInfo(@"response %@",mappingResult);
                                                         }
                                                              failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                             RKLogInfo(@"error response %@", error.localizedDescription );
                                                         }];

1 个答案:

答案 0 :(得分:1)

您应该使用路由,以便可以使用路径模式插入会话ID。然后,您可以使用映射元数据访问映射中的id值,并使用它连接到相应的对象。

相关问题