问题
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];
}
回答1:
For the overall status you can create another operation, which can be a block operation, and which uses addDependency:
to ensure that it runs after all of the other operations are complete. Add the dependencies in the loop where you create each delete operation.
For each individual status you need to use setCompletionBlockWithSuccess:failure:
to get feedback about the results.
来源:https://stackoverflow.com/questions/35129928/how-to-trigger-nsoperationqueue-and-get-results-in-block-of-multiple-operation