それぞれの要求で到着した応答を識別する方法を同じサービスに 2 つのポスト呼び出しを行っています。
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 ];
応答が到着すると、ビューコントローラーへのコールバックデリゲートがあります。同じ関数応答コールバックを同じブロックに使用しているためです。