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

标签 ios iphone objective-c restkit restkit-0.20

我有以下 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/

相关文章:

ios - ARC下iOS内存管理方法

iphone - 为什么我的函数被调用了两次?

objective-c - 以步进增量滚动水平 UIScrollView?

ios - 在展开可选值 WKWebView 获取参数时意外发现 nil

iphone - 当可以使用开发配置文件进行分发时,为什么需要临时分发配置文件?

ios - NSURLConnection connectionWithRequest :delegate and sendAsynchronousRequest:queue:completionHandler? 之间的主要区别是什么

objective-c - IOS Core 数据获取请求 - 排序

ios - 如何使用 Xamarin forms ios PickerRenderer 更改 Picker 字体大小?

iphone - PhoneGap 事件未在 iPhone 上触发

iphone - 使用 CoreText 显示 NSAttributedString