我正在对同一服务进行两次发布调用,以如何识别随其各自请求到达的响应。
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];
}
我正在从视图控制器调用相同的函数
[self.service fetchCoursesWithToken:@"ds" courseType:0 ];
[self.service fetchCoursesWithToken:@"ds" courseType:1 ];
当响应到达时,我有回调委托来查看控制器。因为我使用相同的函数响应回调到相同的块。