RestKit & RKDynamicMapping

Posted

技术标签:

【中文标题】RestKit & RKDynamicMapping【英文标题】: 【发布时间】:2015-09-29 09:34:20 【问题描述】:

我需要使用 restkit 将 json 映射到 coredata。必须根据字段“class”进行映射,根据字段“class”可以填充不同。

例如

我有 json


    "list": [
        
            "uid": "rss_001",
            "message": "messge_001",
            "class": "A",
            "promo": 
                "uid": "promo_001",
                "message": "promo_msg_001",
                "bar": "0001-0001-0001-0001"
            
        ,
        
            "uid": "rss_002",
            "message": "messge_002",
            "class": "B",
            "user": 
                "uid": "user_002",
                "avatar": "avatar_002",
                "gender": "male",
            
        ,
    ],
    "meta": 
        "timestamp": 2009392012,
        "statusCode": 200
    

我的 object-c 映射

    RKEntityMapping *listMapping = [RKEntityMapping mappingForEntityForName:@"List" inManagedObjectStore:RKAPIClient_ManagedObjectStore];
    listMapping.identificationAttributes = @[@"uid"];
    [listMapping addAttributeMappingsFromDictionary:@
                                                     @"uid": @"uid",
                                                     @"message": @"message",
                                                     @"class": @"dataType",
                                                     @"likedNumber": @"likedNumber",
                                                     ];

    RKEntityMapping *promoMapping = [RKEntityMapping mappingForEntityForName:@"Promo" inManagedObjectStore:RKAPIClient_ManagedObjectStore];
    promoMapping.identificationAttributes = @[@"uid"];
    [promoMapping addAttributeMappingsFromDictionary:@
                                                     @"uid": @"uid",
                                                     @"message": @"message",
                                                     @"bar": @"bar",
                                                     ];

    RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:RKAPIClient_ManagedObjectStore];
    userMapping.identificationAttributes = @[@"uid"];
    [userMapping addAttributeMappingsFromDictionary:@
                                                     @"uid": @"uid",
                                                     @"avatar": @"avatar",
                                                     ];

     RKDynamicMapping* dynamicMapping = [RKDynamicMapping new];
[dynamicMapping setObjectMapping:promoMapping whenValueOfKeyPath:@"class" isEqualTo:@"A"];
[dynamicMapping setObjectMapping:userMapping whenValueOfKeyPath:@"class" isEqualTo:@"B"];


 RKResponseDescriptor *listResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:listMapping
                                           method:RKRequestMethodGET
                                      pathPattern:@"list"
                                          keyPath:@"list"



statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [RKAPIClient_ObjectManager addResponseDescriptor:listResponseDescriptor];

但它不起作用 如何正确设置属性动态映射?

【问题讨论】:

看起来一般,你有什么问题/你看到错误/你得到日志输出? 我得到正确的映射“列表”,但没有映射“促销”和“用户”,也没有映射链接“列表->促销”和“列表->用户” 我觉得只设置一个类dynamicMapping是不够的。 ti 有 "class" 匹配选项,但它是如何与 "List" 关联的 - 未指定 我发现了。如果使用[listMapping addRelationshipFromKeyPath:nil toKeyPath:@"subcontent" withMapping:dynamicMapping];,则在dynamicMapping中接收对象“列表”并在“类”中正确匹配,但需要修复映射字典 promo 中的示例修复 ` [promoMapping addAttributeMappingsFromDictionary:@ @"promo.uid": @"uid", @"promo.message": @"message", @"promo.bar": @"bar", ];` 【参考方案1】:

我迅速制定了这个解决方案,针对您的场景进行了重新访问。 希望对你有帮助。

lazy var dynamicMapping: RKDynamicMapping =  [unowned self] in
        let dynMapping = RKDynamicMapping();

        let typeAMatcher:RKObjectMappingMatcher = RKObjectMappingMatcher(keyPath: "class", expectedValue: "A", objectMapping: promoMapping);
        dynMapping.addMatcher(typeAMatcher);

        let typeBMatcher:RKObjectMappingMatcher = RKObjectMappingMatcher(keyPath: "class", expectedValue: "B", objectMapping: userMapping);
        dynMapping.addMatcher(typeBMatcher);

        return dynMapping;
    ()

【讨论】:

以上是关于RestKit & RKDynamicMapping的主要内容,如果未能解决你的问题,请参考以下文章

Restkit:与 xml 的对象映射

RestKit & Cocoapods:映射操作失败/没有找到任何可映射的值

CoreData & RestKit - 从多个来源获取相同的实体类型

RestKit – 使用关系映射将单个源映射到多个目的地

Rails 3.1 & RestKit 0.9.3 - 引用映射

Restkit 0.10.0 & ios 6.1 - objectLoaderWithResourcePath:delegate 方法不进行 http 调用