RestKit动态映射关系名称基于值

7

我正在使用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 的值设置目标关系的名称。
有什么想法吗?

我希望有人在这里回答实际问题。 - magma
3个回答

1

0

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

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

现在我该如何设置Event实体,以便它使用正确的关系? - brynbodayle

0
我最近遇到了这个问题。通过跟踪 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;
            }
        }

...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接