识别当前到达的Response restkit v0.20的Request对象

时间:2013-03-29 06:41:57

标签: iphone ios xcode restkit

我正在对同一服务进行两次调用后如何识别随其请求到达的响应。

req1 = post {"status" = "1"} 
req2 = post {"status" = "0"}

response {"data" = "xxx"}
response {"data" = "yyy"}

如何识别req1和req2的响应。 我正在使用restkit v0.20。

-(void)fetchCoursesWithToken:(NSString *)token  courseType:(NSInteger)courseType{
AppSettings *settings = [AppSettings sharedAppSettings];
NSString *userCourseServiceURL = settings.getCoursesURL;

RKObjectManager *manager = [RKObjectManager sharedManager];

[settings showActivityIndicatorWithMessage:@""];



CourseRequest *reqObj = [CourseRequest new];
reqObj.loginToken = token;
reqObj.courseType = [NSString stringWithFormat:@"%d",courseType];







RKManagedObjectRequestOperation *operation = [manager appropriateObjectRequestOperationWithObject:reqObj method:RKRequestMethodPOST path:userCourseServiceURL parameters: nil];

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    [settings removeActivityIndicator];

    NSHTTPURLResponse *response = [[operation HTTPRequestOperation] response]; //operation is an RKObjectRequestOperation

    NSDictionary *headerDictionary = [response allHeaderFields];

    NSString *status = [headerDictionary objectForKey:@"X-NTST-Request-Status"];

    if ([status isEqualToString:@"ERR"]) {
        NSString *errorMessage = [headerDictionary objectForKey:@"X-NTST-Request-Note"];
        NSLog(@"Error Occured :%@",errorMessage);
    }
    else if ([status isEqualToString:@"WARN"]) {
        NSString *errorMessage = [headerDictionary objectForKey:@"X-NTST-Request-Note"];
        NSLog(@"Error Occured :%@",errorMessage);
    }
    if ([status isEqualToString:@"OK"]) {
        NSLog(@"Succes status:%@",status);
        arr = [[NSMutableArray alloc] initWithArray:[mappingResult array]];
        [self.delegate responseArrived:arr];
    }



} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"It Failed: %@", error);
    [settings removeActivityIndicator];
}];
[RKObjectManager.sharedManager enqueueObjectRequestOperation:operation];
 }

我正在调用同一个函数 来自View controller

 [self.service fetchCoursesWithToken:@"ds"    courseType:0 ];


[self.service fetchCoursesWithToken:@"ds"    courseType:1 ];

当响应到达时,我有回调委托来查看控制器。 因为我使用相同的函数响应回调到同一个块。

1 个答案:

答案 0 :(得分:1)

Restkit是基于块的,因此当您发出请求时,您可以传递一个块(实际上是两个,一个用于成功,一个用于失败),以便在响应到来时执行。

由于块与各个请求相关联,因此无需区分。