我有以下JSON:
"Menus":{
"Id" : "3",
"Name" : "Cheese Burger",
"Items": []
}
我将响应映射到Core Data,如下所示:
+ (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,但在这种情况下它似乎不起作用。