RestKit 0.2 * - 使用RKRelationshipMapping映射多级关系

时间:2014-07-17 20:17:01

标签: ios objective-c restkit restkit-0.20

所以有一个像这样的JSON文件:

{
  "eventList" : [
    {
      "eventID" : 1,
      "comment" : "Connected at place.",
      "eventTypes" : [
        {
          "eventContextTypes" : [
            {
              "eventContextTypeID" : 14,
              "eventContextTypeText" : "Hospital",
            }
          ],
          "eventTypeID" : 2,
          "eventTypeText" : "Connection"
        }
      ],
      "submittedDate" : "2014-07-16T20:24:04Z"
    },
    {
      "eventID" : 2,
      "comment" : "Connected at place.",
      "eventTypes" : [
        {
          "eventContextTypes" : [
            {
              "eventContextTypeID" : 11,
              "eventContextTypeText" : "Location"
            }
          ],
          "eventTypeID" : 2,
          "eventTypeText" : "Connection"
        }
      ],
      "submittedDate" : "2014-06-16T20:10:51Z"
    }
  ]
}

在我的应用中,我有多个来电,其中有EventType来电,其映射如下:

[eventTypeMapping addPropertyMappingsFromArray:@[[RKRelationshipMapping relationshipMappingFromKeyPath:EventTypeRelationships.eventContextTypes
                                                                                             toKeyPath:EventTypeRelationships.eventContextTypes
                                                                                           withMapping:eventContextTypeMapping]]];

然后是eventList调用,映射是这样的:

[newsFeedMapping addPropertyMappingsFromArray:@[[RKRelationshipMapping relationshipMappingFromKeyPath:@"eventList"
                                                                                            toKeyPath:@"eventList"
                                                                                          withMapping:eventTypeMapping]]];

问题在于映射eventContextTypes的值。由于某种原因,它只保存映射的最后一个对象的映射数据。例如,当我调用JSON文件时,它会映射Hospital,然后用第一个对象的Location(数组中的第二个值)覆盖它。我在这里做错了什么?

提前致谢。

EventTypeMapping和EventContextMapping的定义如下:

RKObjectMapping *eventTypeMapping = [EventType mapping];
RKObjectMapping *eventContextTypeMapping = [EventContextType mapping];

======更新======

以下是EventTypesEventContextType

的映射功能
// EVENTTYPE

+ (RKObjectMapping *)mapping {
    // Create a singleton instance of the mapping object.
    __strong static RKEntityMapping *_mapping = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        RKManagedObjectStore *store = [[RKObjectManager sharedManager] managedObjectStore];
        _mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:store];

        // Map attributes with the same name in the JSON and the model.
        // Use the string constants provided by mogenerator.
        [_mapping addAttributeMappingsFromDictionary:@{@"eventTypeID" : EventTypeAttributes.eventTypeID,
                                                       @"eventTypeText" : EventTypeAttributes.eventTypeText}];

        // Set primaryKeyAttribute
        _mapping.identificationAttributes = @[EventTypeAttributes.eventTypeID];

        // Relationships are mapped elsewhere
    });
    return _mapping;
}

EventContextType映射:

+ (RKObjectMapping *)mapping {
    // Create a singleton instance of the mapping object.
    __strong static RKEntityMapping *_mapping = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        RKManagedObjectStore *store = [[RKObjectManager sharedManager] managedObjectStore];
        _mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:store];

        // Map attributes with the same name in the JSON and the model.
        // Use the string constants provided by mogenerator.
        [_mapping addAttributeMappingsFromDictionary:@{@"eventContextTypeID" : EventContextTypeAttributes.eventContextTypeID,
                                                       @"eventContextTypeText" : EventContextTypeAttributes.eventContextTypeText}];

        // Set primaryKeyAttribute
        _mapping.identificationAttributes = @[EventContextTypeAttributes.eventContextTypeID];

        // Relationships are mapped elsewhere
    });
    return _mapping;
}

以下是模型设计:

enter image description here

=======更新结束=======

0 个答案:

没有答案