2015-10-19 46 views
0

我在我的项目中的各个类中有几个方法,分别是methodA(),methodB(),methodC()... methodZ()。每种方法都使用NSOperation执行网络调用。有些情况下我必须并行执行方法,如方法A,D,M应该并行执行。在另一种情况下说,方法D,S,T应该并行执行。我在APIManager类中维护一个执行我所有方法的常用方法。并行执行多个NSOperation

我试着在APIManager类中创建一个操作队列,但它不工作。只有方法执行完成后,才会执行另一个方法执行。任何人都可以在这方面建议?

-(void) methodA { 

NSString *path = [NSString stringWithFormat:kPath, @“Function1”]; 

NSString *requestXML = [NSString stringWithFormat:kGetFunction1RequestXML]; 

self.operation = [self.apiMgr requestWithPath:path method:@"POST" xml:requestXML headers:@{@"Accept": @"application/xml", @"Content-Type": @"application/xml"} 

             success:^(id response) { 

              NSLog(@“Request successful. Do further handling”);            
             } 

             failure:^(NSError *error) { 
              NSLog(@“failed”);            
             }]; 

}

- (无效)的methodB {

NSString *path = [NSString stringWithFormat:kPath, @“Function2”]; 

NSString *requestXML = [NSString stringWithFormat:kGetFunction2RequestXML]; 

self.operation = [self.apiMgr requestWithPath:path method:@"POST" xml:requestXML headers:@{@"Accept": @"application/xml", @"Content-Type": @"application/xml"} 

             success:^(id response) { 

              NSLog(@“Request successful. Do further handling”);            
             } 

             failure:^(NSError *error) { 
              NSLog(@“failed”);            
             }]; 

}

- (id)requestWithPath:(NSString *)path method:(NSString *)method xml:(NSString *)requestXML headers:(NSDictionary *)headers success:(void(^)(id response))success failure:(void(^)(NSError *error))failure 

{

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@“%@, self.serverAddress]]; 

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:url]; 

if (headers) { 
    for (NSString *header in headers) { 
     [client setDefaultHeader:header value:headers[header]]; 
    } 
} 

[client setParameterEncoding:AFJSONParameterEncoding]; 

NSMutableURLRequest *request = nil; 

request = [client requestWithMethod:method path:path parameters:nil]; 

[request setHTTPBody:[requestXML dataUsingEncoding:NSUTF8StringEncoding]]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[self.operationQueue addOperation:operation]; 

[operation setAuthenticationAgainstProtectionSpaceBlock:^BOOL(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace) 
{ 
    return YES; 
}]; 

[operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) 
{ 
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
}]; 

[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ 
    LogVerbose(@"Background time expired for AFNetworking..."); 
}]; 


[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSString *xmlStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; 
    NSDictionary *xmlDic = [NSDictionary dictionaryWithXMLString:xmlStr]; 

    if (success) 
     success(xmlDic); 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    if (failure) { 

     failure(error); 
    } 
}]; 

return nil; 

}

+0

尝试在NSOperationQueue实例上将maxConcurrentOperationCount设置为大于1的数字。如果未解决,系统应该决定它可以一次处理多少个操作。 – Greg

+0

@Greg,也试过这样做。尽管如此,这些行动还是一个又一个的发生。 –

+0

这个家伙已经很好地解释了NSOperation,请通过这个:https://izeeshan.wordpress.com/2014/08/17/multi-threading-using-nsoperation/ – manish

回答

0

你没有分享你正在实施的任何代码。请描述性。

NSOperationsQueue只接受NSOperation,您将不得不提供MaxConcurrentOperationCount属性来告诉队列要并行执行多少操作。在你的情况下,你定义了方法:methodA(),methodB(),methodC()... methodZ()。每种方法都使用NSOperation执行网络呼叫。但是你在NSOperationsQueue中加入了什么,你还没有提到。

请加一些代码让我明白..

谢谢!!!

+1

我有编辑我的问题。你能否核实并提供你的意见。我已经提供了MaxConcurrentOperationCount为100. –

+0

在代码中,我找不到'MaxConcurrentOperationCount' .. – NSPratik

+1

我在init方法'self.operationQueue = [[NSOperationQueue alloc] init]; self.operationQueue.maxConcurrentOperationCount = 100;' –