【问题标题】:RestKit Dynamically Map Relationship Name based on ValueRestKit 根据值动态映射关系名称
【发布时间】:2012-10-21 12:14:27
【问题描述】:

我正在使用 RestKit 解析 JSON 并将其映射到 Core Data NSManagedObjects。这是一个示例 JSON。

{
    "events": [
        {
            "description": "...",
            "subject_type": "photo",
            "subject": {
                "id": 1,
                "thumb_url": "...",
                "medium_url": "...",
                "large_url": "..."
            }
        },
        {
            "description": "...",
            "subject_type": "user",
            "subject": {
                "id": 1,
                "username": "...",
                "followers": "..."
            }
        }
    ]
}

使用RKObjectMappingProviderRKManagedObjectMapping 我将"events" 数组映射到单独的Core Data Event 对象中。这很好用。

现在Event 有两个关系UserPhoto。现在我需要根据"subject_type" 的值将主题数组映射到正确的Core Data 对象,并将其设置为Event 上的正确关系。

我尝试使用RKDynamicObjectMapping,但我不知道如何为“动态关系”指定它。我需要一些方法来根据subject_type 的值设置目标关系的名称。

有什么想法吗?

【问题讨论】:

  • 我希望有人在这里回答实际问题。

标签: iphone objective-c ios json restkit


【解决方案1】:

在 RestKit 中,RKDynamicMapping 还允许使用指定的块来执行更复杂的动态映射操作,就像您描述的那样。提供这种能力的具体方法称为setObjectMappingForRepresentationBlock

以下是有关该方法的一些文档: http://restkit.org/api/latest/Classes/RKDynamicMapping.html#//api/name/setObjectMappingForRepresentationBlock:

RestKit 单元测试说明了此方法的一些简单案例。

【讨论】:

    【解决方案2】:

    看来你应该可以使用RKDynamicObjectMapping(见Object Mapping)。您可以创建一个用户映射和一个照片映射,然后像这样使用您的RKDynamicObjectMapping

    [dynamicMapping setObjectMapping:userMapping 
                  whenValueOfKeyPath:@"subject_type" 
                           isEqualTo:@"user"];
    [dynamicMapping setObjectMapping:photoMapping 
                  whenValueOfKeyPath:@"subject_type" 
                           isEqualTo:@"photo"];
    

    【讨论】:

    • 现在如何在Event 实体上设置它,以便它使用正确的关系?
    【解决方案3】:

    我最近遇到了这个问题。通过 RestKit 进行跟踪,看起来它正在尝试将所有对象映射应用于 RKDynamicMapping 实例中的所有关系。请参阅 RKMappingOperation.m 中的 applyRelationshipMappings

    我想出了一个 hack 的解决方案,但不需要我大量修改 restkit 代码。

    在RKMappingOperation.m中,我修改了如下方法:

    destinationObjectForMappingRepresentation:parentRepresentation:withMapping:inRelationship:
    

    我添加了以下代码(从注释开始)检查关系的目标是否与正在应用的对象类型相同,并且仅在匹配时继续。 (这没有经过严格的测试,但在我的特定用例中有效。)

    - (id)destinationObjectForMappingRepresentation:(id)representation parentRepresentation:(id)parentRepresentation withMapping:(RKMapping *)mapping inRelationship:(RKRelationshipMapping *)relationshipMapping
    {
        RKObjectMapping *concreteMapping = nil;
        if ([mapping isKindOfClass:[RKDynamicMapping class]]) {
            concreteMapping = [(RKDynamicMapping *)mapping objectMappingForRepresentation:representation];
            if (! concreteMapping) {
                RKLogDebug(@"Unable to determine concrete object mapping from dynamic mapping %@ with which to map object representation: %@", mapping, representation);
                return nil;
            }
            // Make sure the destination of a core data relationship is the right entity class for the object we're mapping;
            if ([self.destinationObject respondsToSelector:@selector(managedObjectContext)])
            {
                NSEntityDescription *destinationEntity = [self.destinationObject entity];
                NSDictionary *destinationPropertiesDictionary = [destinationEntity propertiesByName];
                NSRelationshipDescription *destinationPropertyForDestinationKeyPath = [destinationPropertiesDictionary valueForKey:relationshipMapping.destinationKeyPath];
                NSString *relationshipDestinationClassName = [[destinationPropertyForDestinationKeyPath destinationEntity] name];
                NSString *mappedObjectClassName = [NSString stringWithCString:class_getName(concreteMapping.objectClass) encoding:NSUTF8StringEncoding];
                if (![relationshipDestinationClassName isEqualToString:mappedObjectClassName])
                {
                    return nil;
                }
            }
    

    ...

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      相关资源
      最近更新 更多