如何使用restkit忽略已发布对象的空属性

如何使用restkit忽略已发布对象的空属性

本文介绍了如何使用restkit忽略已发布对象的空属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在恢复一个最初使用 RestKit 0.10 的旧项目,现在正在使用 RestKit 0.24.旧版本仍然有效,但不幸的是 RestKit 0.10 不兼容 64 位,因此不会提交到 AppStore(无论如何肯定是时候更新了).

I am reviving an old project that originally used RestKit 0.10, and now am using RestKit 0.24. The old version still works, but unfortunately RestKit 0.10 is not 64-bit compatible and hence does not submit to the AppStore (and it is certainly time to update anyway).

我无法正确发布对象.在 RestKit 0.10 中,没有值的属性不会发送到服务器,而在 RestKit 0.20 中它们似乎是.我已经尝试将 assignsDefaultValueForMissingAttributes 显式设置为 NO,但它似乎没有什么区别.

I cannot get an object to post correctly. In RestKit 0.10, properties without values were not sent to the server, whereas it seems in RestKit 0.20 they are. I have tried explicitly setting assignsDefaultValueForMissingAttributes to NO, but it doesn't seem to make a difference.

服务器需要以下格式:

{"response": {"assessment_id":"1","time_taken":"60"},
 "answer": [
      {"question_number": 1, "answer_value": 3},
      {"question_number": 2, "answer_value": 2},
      {"question_number": 3, "answer_value": 1},
 ]
}

我设置了一个对象 CompletedAssessment,其中包含一个 Response 对象和一个 Answer 对象数组.(请注意,当从服务器接收这些对象时,需要接收的属性比需要发送的要多得多).

I have set up an object CompletedAssessment which contains a Response object and an array of Answer objects. (Note that when these objects are received from the server, many more properties need to be received than need to be sent).

@interface CompletedAssessment : NSObject {
    Response *response;
    NSArray *answers;
}

@interface Answer : NSObject {
    NSNumber *identifier;
    NSNumber *responseId;
    NSNumber *questionNumber;
    NSString *answerHistory;
    NSString *answerValue;
    NSString *answerText;
    NSNumber *timeTaken;
}

@interface Response : NSObject {
    NSNumber *identifier;
    NSNumber *assessmentId;
    NSNumber *timeTaken;
    NSNumber *clientId;
    NSString *assessmentShortName;
    NSString *score;
    NSString *interpretation;
    NSString *dateCreated;
    NSString *localTime;

}

我按如下方式设置映射:

I set the mapping up as follows:

RKObjectMapping *answerMapping = [RKObjectMapping mappingForClass:[Answer class]];
answerMapping.assignsDefaultValueForMissingAttributes = NO;

[answerMapping addAttributeMappingsFromDictionary:@{
                                                  @"id": @"identifier",
                                                  @"response_id": @"responseId",
                                                  @"question_number": @"questionNumber",
                                                  @"answer_history": @"answerHistory",
                                                  @"answer_value": @"answerValue",
                                                  @"answer_text": @"answerText",
                                                  @"time_taken": @"timeTaken"
                                                  }];

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Response class]];
responseMapping.assignsDefaultValueForMissingAttributes = NO;

[responseMapping addAttributeMappingsFromDictionary:@{
                                                   @"id": @"identifier",
                                                   @"client_id": @"clientId",
                                                   @"assessment_id": @"assessmentId",
                                                   @"time_taken": @"timeTaken",
                                                   @"score": @"score",
                                                   @"assessment_short_name": @"assessmentShortName",
                                                   @"interpretation": @"interpretation",
                                                   @"created": @"dateCreated",
                                                   @"local_time": @"localTime"
                                                   }];

RKObjectMapping *completedAssessmentMapping = [RKObjectMapping mappingForClass:[CompletedAssessment class]];
completedAssessmentMapping.assignsDefaultValueForMissingAttributes = NO;

[completedAssessmentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response" toKeyPath:@"response" withMapping:responseMapping]];
[completedAssessmentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"answer" toKeyPath:@"answers" withMapping:answerMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:completedAssessmentMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"data.completedAssessment" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[completedAssessmentMapping inverseMapping] objectClass:[CompletedAssessment class] rootKeyPath:nil method:RKRequestMethodPOST];
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];

[objectManager.router.routeSet addRoute:[RKRoute
                                         routeWithClass:[CompletedAssessment class]
                                         pathPattern:@"clients/:response.clientId/responses"
                                         method:RKRequestMethodPOST]] ;

日志显示最终 JSON 以这种格式出现:

Logging reveals the end JSON appears in this format:

{"response":
    {"interpretation":null,"id":null,"score":null,"client_id":15,"local_time":"2015-8-6 13:8:34","time_taken":5,"assessment_short_name":null,"assessment_id":8,"created":null},
"answer":[
    {"answer_value":"0","id":null,"answer_text":null,"answer_history":null,"time_taken":null,"response_id":null,"question_number":1},
    {"answer_value":"1","id":null,"answer_text":null,"answer_history":null,"time_taken":null,"response_id":null,"question_number":2}
]}

和 RestKit 日志确认空映射:

And RestKit logging confirms the null mapping:

restkit.object_mapping:RKMappingOperation.m:873 Mapped relationship object from keyPath 'response' to 'response'. Value: {
"assessment_id" = 8;
"assessment_short_name" = "<null>";
"client_id" = 15;
created = "<null>";
id = "<null>";
interpretation = "<null>";
"local_time" = "2015-8-6 13:8:34";
score = "<null>";
"time_taken" = 5;
}

restkit.object_mapping:RKMappingOperation.m:715 Mapped attribute value from keyPath 'identifier' to 'id'. Value: (null)
...

请帮忙!

推荐答案

您正在创建一个新的映射,在此行中调用 [selfCompletedAssessmentMapping inverseMapping]:

You are creating a new mapping calling [selfCompletedAssessmentMapping inverseMapping] in this line:

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[selfCompletedAssessmentMapping inverseMapping] objectClass:[CompletedAssessment class] rootKeyPath:nil method:RKRequestMethodPOST];

将其保存到一个变量中,并在创建描述符之前将 assignsDefaultValueForMissingAttributes 赋值给 NO:

Save it to a variable and assign assignsDefaultValueForMissingAttributes to NO before creating the descriptor:

RKObjectMapping *requestMapping = [selfCompletedAssessmentMapping inverseMapping];
requestMapping.assignsDefaultValueForMissingAttributes = NO;
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[CompletedAssessment class] rootKeyPath:nil method:RKRequestMethodPOST];

这篇关于如何使用restkit忽略已发布对象的空属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:12