我有以下 JSON:
"Menus":{
"Id" : "3",
"Name" : "Cheese Burger",
"Items": []
}
我像这样将响应映射到核心数据:
+ (RKEntityMapping *)menuMapping {
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Menu" inManagedObjectStore:[[CoreDataManager sharedInstance] objectStore]];
mapping.assignsNilForMissingRelationships = YES;
mapping.assignsDefaultValueForMissingAttributes = YES;
[mapping addAttributeMappingsFromDictionary:@{
@"Id": @"remoteID",
@"Name": @"name",
}];
mapping.identificationAttributes = @[ @"remoteID" ];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Items"
toKeyPath:@"products"
withMapping:[MappingProvider productMapping]]];
return mapping;
}
+ (RKEntityMapping *)productMapping {
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Product" inManagedObjectStore:[[CoreDataManager sharedInstance] objectStore]];
mapping.assignsNilForMissingRelationships = YES;
[mapping addAttributeMappingsFromDictionary:@{
@"Id": @"remoteID",
@"Name": @"name",
@"Description" : @"info",
@"Price": @"price"
}];
mapping.identificationAttributes = @[ @"remoteID" ];
return mapping;
}
我将如何设法验证 Items 数组是否为空,并根据 Items(产品)是否包含值在 Core Data 中创建 Menu 对象?我曾尝试使用 assignsNilForMissingRelationships,但在这种情况下它似乎不起作用。
最佳答案
使用KVC validation分析传入的值并(修改或)拒绝它。
关于ios - 使用 Restkit 忽略映射中的空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22576837/