如何触发NSOperationQueue并在多个操作块中获得结果

时间:2016-02-01 11:55:37

标签: ios objective-c nsoperationqueue

我已经问过与foo(); bar(); function foo() { function bar() { echo "I don't exist until foo() is called.\n"; } } 相关的问题,但我仍然在用多次操作实现操作队列。我有以下代码

NSOperationQueue

上面的代码在循环中创建操作并添加到 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; } 中,然后将此操作数组添加到operationArray中。现在我想触发它​​并得到整个数组的响应。

被修改

operationQueue

}

1 个答案:

答案 0 :(得分:0)

对于整体状态,您可以创建另一个操作,该操作可以是块操作,并使用addDependency:确保在所有其他操作完成后运行。在创建每个删除操作的循环中添加依赖项。

对于每个个人状态,您需要使用setCompletionBlockWithSuccess:failure:获取有关结果的反馈。