问题是我需要删除属性@" 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;
}
答案 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;
}