ios - 使用休息套件第二次请求后响应不正确

标签 ios restkit-0.20

我是restkit新手,对此我有一个问题。我有一个用于 checkin 的休息服务。所以我用“Put”请求调用我的休息服务。看起来像 Put: http: .../api/v1/locations/location/[Location-ID]/checkin?userId=[User-ID]。响应是 a ,所以我将其称为 checkin 对象。办理入住手续后,我正在调用另一个服务(Get-Request)。但是 get-request 的新映射结果包含之前的 checkin 对象。我如何重置/删除它?

[self.restHelper checkinUser:[NSNumber numberWithInt:USER_ID] forLocationID:self.selectedLocation success:successBlock failure:failureBlock];

映射:

- (void)checkinUser:(NSNumber*) userID forLocationID:(NSNumber *)locationID success:(void (^)(RKObjectRequestOperation *, RKMappingResult *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure{

RKObjectManager *objectManager = [RKObjectManager sharedManager];

RKObjectMapping *userCheckinMapping = [RKObjectMapping mappingForClass:[CheckIN class]];
[userCheckinMapping addAttributeMappingsFromDictionary:@{
 @"locationFullname" : @"locationFullname",
 @"userNameCityCountry" : @"userNameCityCountry",
 @"success" : @"success",
 }];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userCheckinMapping pathPattern:nil keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:responseDescriptor];

NSString *path = [NSString stringWithFormat:@".../api/v1/locations/location/%@/checkin?userId=%@", locationID,userID];

[objectManager putObject:nil path:path parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    success(operation, mappingResult);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    failure(operation,error);
}];    

下一个调用是这样的:

 -(void)getTracksForLocation:(NSNumber *)locationID withType:(NSString*) type success:(void (^)(RKObjectRequestOperation *, RKMappingResult *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure{

RKObjectManager *objectManager = [RKObjectManager sharedManager];

RKObjectMapping *trackResultsMapping = [RKObjectMapping mappingForClass:[RESTTracksResult class]];
[trackResultsMapping addAttributeMappingsFromDictionary:@{
 @"id" : @"ID",
 @"cued" : @"cued",
 @"marked" : @"marked",
 @"playing": @"playing",
 @"played": @"played",
 @"creditBidSum": @"creditBidSum",
 }];

RKObjectMapping *trackMapping = [RKObjectMapping mappingForClass:[RESTTrack class]];
[trackMapping addAttributeMappingsFromDictionary:@{
 //@"id" : @"ID",
 @"name" : @"name",
 @"artistName" : @"artistName",
 @"genre": @"genre",
 @"length": @"length",
 }];

RKRelationshipMapping* relationShipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"track"
                                                                                         toKeyPath:@"track"
                                                                                       withMapping:trackMapping];

[trackResultsMapping addPropertyMapping:relationShipMapping];


RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:trackResultsMapping pathPattern:nil keyPath:@"results" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:responseDescriptor];

NSDictionary *trackDictionary = @{ @"type": type};

NSString *path = [NSString stringWithFormat:@".../api/v1/locations/location/%@/tracks", locationID ];

[objectManager getObjectsAtPath:path parameters:trackDictionary success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    success(operation, mappingResult);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    failure(operation,error);
}];

希望你能帮助我,ty!我也尝试过删除缓存,但没有成功!

最佳答案

您做错了一些事情,这意味着当您进行第二次调用时,第一次调用的所有配置都将用于尝试理解第二个响应。

1.

当您使用对象管理器时,您应该设置基本 URL。你可能已经在这样做了,这很难说。然后,每当您指定路径时,它都应该相对于该基点。

2.

创建路径时,请勿添加参数。这就是 put 和 get 方法中 parameters 参数的作用。

3.

如果您使用sharedManager,您应该一次性完成所有映射配置。不要在每次调用方法时添加额外的映射。

4.

创建响应描述符时,设置路径模式和关键路径。这是你的主要问题。如果你不设置它们,RestKit 将始终尝试使用机架映射来处理每个响应。这将导致您在映射结果中获得意外的对象,可能会赢得部分信息。

关于ios - 使用休息套件第二次请求后响应不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17381680/

相关文章:

ios - RestKit:如何使用路由发布对象?

macos - Restkit:迁移到 0.20

ios - Restkit 0.2 加载两个具有相同实体和不同谓词的不同表时遇到问题

ios - 如何获取KVO#keyPath(WKWebView.url)的URL值?

ios - 将 RestKit 从 0.10 升级到 0.20 的问题

ios - 从当前 View 和推送 View 处理后退按钮

php - AFNetworking 2.0 多部分/表单数据上传到 mySQL

ios - 使用 Restkit 从 JSON 数组映射关系

ios - 从选定的表格 View 单元格创建一个组

ios - 如何隐藏 UIView 并删除 "empty"空间? -iOS/Monotouch