使用Restkit忽略映射中的空数组

时间:2014-03-22 11:13:13

标签: ios iphone objective-c restkit restkit-0.20

我有以下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,但在这种情况下它似乎不起作用。

1 个答案:

答案 0 :(得分:2)

使用KVC validation分析传入值并(修改或)拒绝它。