【问题标题】:Ignoring default value for missed elements instead of setting nil with RestKit忽略缺失元素的默认值,而不是使用 RestKit 设置 nil
【发布时间】:2017-06-06 19:09:04
【问题描述】:

我尝试发送没有特定字段的响应,即使在我使用以下方法禁用此选项后,RestKit 仍将值设置为 nil:

infoMapping.assignsDefaultValueForMissingAttributes = NO;

完整的映射代码:

RKEntityMapping *infoMapping = [RKEntityMapping mappingForEntityForName:@"IndexInfo" inManagedObjectStore:managedObjectStore];
    //userMapping.identificationAttributes = @[@"id"];
    [infoMapping addAttributeMappingsFromDictionary:@{
                                                      @"first" : @"first",
                                                      @"last" : @"last"
                                                      }];

    infoMapping.assignsDefaultValueForMissingAttributes = NO;

RKResponseDescriptor *infoResponseDescriptor =
    [RKResponseDescriptor responseDescriptorWithMapping:infoMapping
                                                 method:RKRequestMethodGET
                                            pathPattern:@"/core/feed.json"
                                                keyPath:@"info"
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)

我确实需要能够使用部分数据进行响应,如果这是一个错误,是否有另一种方法可以绕过它来挂钩映射以手动禁用它?

【问题讨论】:

    标签: objective-c core-data restkit


    【解决方案1】:

    事实证明,它每次都在为我的 infoMapping 创建新实体。

    因此,一种解决方法 iv 在响应中包含了一个 id 属性,映射了这个添加并将其用作标识属性。这可能不是最好的解决方案,但它现在有效。 (所以我的 id 在我的回复中总是 0)

    我还在我的 IndexInfo 模型中加入了一些验证。

    infoMapping.identificationAttributes = @[@"id"];
        [infoMapping addAttributeMappingsFromDictionary:@{
                                                          @"id" : @"id",
                                                          @"first" : @"first",
                                                          @"last" : @"last"
                                                          }];
    

    验证:

    - (BOOL)validateFirst:(id *)ioValue error:(NSError **)outError { ....
    

    我也需要:

    infoMapping.performsKeyValueValidation = YES;
    infoMapping.discardsInvalidObjectsOnInsert = YES;
    

    代替

    //    infoMapping.assignsDefaultValueForMissingAttributes = NO;
    

    id 从未使用过,所以我会想出一个更优雅的方法并更新答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      相关资源
      最近更新 更多