第二次请求后使用rest kit不正确响应

时间:2013-06-29 15:17:11

标签: ios restkit-0.20

我是restkit的新手,我有一个问题。我有一个休息服务来办理登机手续。所以我用“Put” - 请求来调用我的restservice。看起来像Put:http:... / api / v1 / locations / location / [Location-ID] / checkin?userId = [User-ID]。响应是a,所以我称之为checkin-object。办理登机手续后,我打电话给另一个服务(Get-Request)。但是get-request的新映射结果包括先前的checkin-object。如何重置/删除它?

[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 个答案:

答案 0 :(得分:1)

你做了一些错误,他们的意思是当你进行第二次调用时,第一次调用的所有配置都被用来试图理解第二次响应。

1

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

2

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

3

如果您使用sharedManager,则应该执行一次所有映射配置。每次调用方法时都不要添加其他映射。

4

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