I already asked question related to NSOperationQueue
but I am still around of implementing operation queue with multiples operation. I have following code
NSMutableArray * operationArray = [[NSMutableArray alloc] init];
for (int i =0; i<[documentModelList count]; i++) {
DocumentModel * documentModel = [documentModelList objectAtIndex:i];
NSString *url = [NSString stringWithFormat:@"%@%@/%li", SERVER_URL, DOCUMENTS_DELETE,(long)documentModel.documentID];
[operationArray addObject:[AppHttpClient getDeleteRequest:nil urlQuery:url]];
}
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
// Set the max number of concurrent operations (threads)
[operationQueue setMaxConcurrentOperationCount:operationArray.count];
[operationQueue addOperations:operationArray waitUntilFinished:NO];
+ (AFHTTPRequestOperation *) getDeleteRequest:(NSDictionary *)headerParams urlQuery: (NSString*)action
{
NSString *jsonString = @"";
NSString *authorizationValue = [self setAuthorizationValue:action];
NSString *language = @"en_US";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:language forHTTPHeaderField:@"Accept-Language"];
[request setValue:authorizationValue forHTTPHeaderField:@"authorization"];
[request setURL:[NSURL URLWithString:action]];
[request setTimeoutInterval:500.0];
[request setHTTPMethod:@"DELETE"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
return operation;
}
The above code creating operations in loop and adding into operationArray
and then add this operation array into operationQueue
. Now I want to trigger that and get response of whole array.
Edited
+ (void) gernalDeleteRequest:(NSDictionary *)headerParams urlQuery: (NSString*)action parameters:(NSDictionary*)params
onComplete:(void (^)(id json, id code))successBlock
onError:(void (^)(id error, id code))errorBlock
{
NSString *jsonString = @"";
NSString *authorizationValue = [self setAuthorizationValue:action];
NSString *language = @"en_US";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:language forHTTPHeaderField:@"Accept-Language"];
[request setValue:authorizationValue forHTTPHeaderField:@"authorization"];
//convert parameters in to json data
if ([params isKindOfClass:[NSDictionary class]]) {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
options:NSJSONWritingPrettyPrinted
error:&error];
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
[request setURL:[NSURL URLWithString:action]];
[request setTimeoutInterval:500.0];
[request setHTTPMethod:@"DELETE"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSInteger statusCode = [operation.response statusCode];
NSNumber *statusObject = [NSNumber numberWithInteger:statusCode];
successBlock(responseObject, statusObject);
NSLog(@"authentication success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSInteger statusCode = [operation.response statusCode];
NSNumber *statusObject = [NSNumber numberWithInteger:statusCode];
id responseObject = operation.responseData;
id json = nil;
NSString *errorMessage = nil;
if (responseObject) {
json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
errorMessage = [(NSDictionary*)json objectForKey:@"Message"];
}else{
json = [error.userInfo objectForKey:NSLocalizedDescriptionKey];
errorMessage = json;
}
errorBlock(errorMessage, statusObject);
}];
[[NSOperationQueue mainQueue] addOperation:operation];
}