RestKit - 使用RKRequestDescriptor中的nil值排除属性映射

时间:2014-06-27 03:46:47

标签: ios objective-c core-data restkit restkit-0.20

问题是我需要删除属性@" unitprice"当值为nil时,完全来自有效负载,但如果请求中有值,则将其保留在那里。因此,OrderLine的有效负载将如下所示:@ {" id":@" orderlineId",@" unitprice":@" unitprice&#34 ;} @ @" id":@" orderlineId"}请注意,映射是一对多的关系。是否有可能做到这一点?非常感谢您的帮助,谢谢! / *  requestDescriptor  * /

+(RKObjectMapping*) getSalesOrderMapping:(RKRequestMethod)method {

RKEntityMapping *requestMapping = [RKEntityMapping mappingForEntityForName:@"SalesOrder"
                                                       inManagedObjectStore:[RKManagedObjectStore defaultStore]];
RKEntityMapping *orderLinesMapping = [RKEntityMapping mappingForEntityForName:@"OrderLine"
                                                         inManagedObjectStore:[RKManagedObjectStore defaultStore]];
requestMapping.identificationAttributes = @[ @"salesOrderId" ];
NSMutableDictionary *attributeMappings = [NSMutableDictionary dictionaryWithDictionary:@{
                                                                                         @"id": @"salesOrderId",
                                                                                         }];
NSDictionary *orderLineAttributeMappings = @{
                                             @"id": @"orderlineId",
                                             @"unitprice": @"unitPrice"
                                             };

[orderLinesMapping addAttributeMappingsFromDictionary:orderLineAttributeMappings];
[requestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"lines"
                                                                                toKeyPath:@"salesOrderToOrderLines"
                                                                              withMapping:orderLinesMapping]];


return  requestMapping;
}

enter image description here

2 个答案:

答案 0 :(得分:1)

在映射上将assignsDefaultValueForMissingAttributes设置为NO。您不应该需要2个不同的映射(除非您确实希望在JSON中包含nil以获取某些属性)。

答案 1 :(得分:0)

似乎无法实现具有1对多关系的动态请求描述符。所以我必须通过检查nil值手动构建NSMutableDictionary,然后添加属性及其值,如下所示

[objectManager postObject:nil
                    path:@"/salesorder"
              parameters:[self customSalesOrderRequestMapping]
                 success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {... }

 - (NSMutableDictionary *) customSalesOrderRequestMapping {

    customSalesOrderAttributeDictionary = [NSMutableDictionary new];

    if (selectedSalesOrder.salesOrderId) [customSalesOrderAttributeDictionary addEntriesFromDictionary:@{@"id": selectedSalesOrder.salesOrderId}];

    if (selectedSalesOrder.salesOrderToOrderLines.count > 0) {

        NSMutableArray *orderLinesMutableArray = [NSMutableArray new];
         for (OrderLine *orderLine in selectedSalesOrder.salesOrderToOrderLines)
        {
            NSMutableDictionary *orderLineMutableDictionary = [NSMutableDictionary new];
            if (orderLine.orderlineId) [orderLineMutableDictionary addEntriesFromDictionary:@{@"id": orderLine.orderlineId}];
            if (orderLine.unitPrice) [orderLineMutableDictionary addEntriesFromDictionary:@{@"unitprice": orderLine.unitPrice}];
            [orderLinesMutableArray addObject:orderLineMutableDictionary];
    }

    [customSalesOrderAttributeDictionary addEntriesFromDictionary:@{@"lines": orderLinesMutableArray}];

  }
    return customSalesOrderAttributeDictionary;
}