前端之家收集整理的这篇文章主要介绍了
多并发条件依赖执行 (仅供自己参考),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
+ (void)searchMutableRequest:(NSArray *)urlArray Page:(int)page FMSSigItem:(FMSSigItem *)item CallBack:(FMSearchCallback)callback{
if (urlArray.count > 0) {
__block NSInteger requestCount = 0;
__block NSMutableArray *mutableResult = [NSMutableArray arrayWithCapacity:urlArray.count];
for (int i = 0; i < urlArray.count; i++) {
[mutableResult addObject:[NSNull null]];
}
//
// dispatch_group_notify(<#dispatch_group_t group#>,<#dispatch_queue_t queue#>,<#^(void)block#>)
//
// dispatch_group_async(<#dispatch_group_t group#>,<#^(void)block#>)
/**
NSOperation *end = [NSBlockOperation blockOperationWithBlock:^{
}];
NSOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
}];
NSOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
}];
[end addDependency:op1];
[end addDependency:op2];
NSArray *ar =@[end,op1,op2];
[[NSOperationQueue mainQueue] addOperations:ar waitUntilFinished:YES];
*/
NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
myQueue.maxConcurrentOperationCount = urlArray.count;
[urlArray enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop) {
NSDictionary *dic = obj;
NSString *urlStr = [dic objectForKey:@"url"];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[FetchMusicSourceAPI setSearchMutableRequest:request Dic:dic];
[NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {
if (connectionError) {
[FMSShowError showNetError:connectionError];
return ;
}
else{
if (data) {
NSString *resultStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(resultStr){
NSLog(@"=====%d",idx);
[mutableResult replaceObjectAtIndex:idx withObject:resultStr];
NSLog(@"fffff");
}
}
}
requestCount++;
}];
}];
while (requestCount < urlArray.count) {
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
FMSSigItem *parseItem = [FMSSigItem itemWithsourceID:item.sourceID Method:item.method Params:item.params Mode:@"parse" Data:mutableResult];
[FetchMusicSourceAPI searchParseWithFMSSigItem:parseItem Page:page CallBack:callback];
}
}
原文链接:https://www.f2er.com/javaschema/285058.html