Ios 并行执行多个操作
在我的项目中,我在不同的类中有几个方法,比如methodA()、methodB()、methodC()。。。methodZ()。每个方法都使用NSO操作执行网络调用。有些情况下,我必须并行执行方法,就像方法A、D、M应该并行执行一样。在另一种情况下,方法D、S、T应该并行执行。我在APIManager类中维护一个公共方法,它执行我的所有方法 我尝试在APIManager类中创建一个操作队列,但它不起作用。只有一个方法执行完毕,另一个方法执行才会发生。在这方面,有人能提出建议吗Ios 并行执行多个操作,ios,objective-c,nsoperation,nsoperationqueue,Ios,Objective C,Nsoperation,Nsoperationqueue,在我的项目中,我在不同的类中有几个方法,比如methodA()、methodB()、methodC()。。。methodZ()。每个方法都使用NSO操作执行网络调用。有些情况下,我必须并行执行方法,就像方法A、D、M应该并行执行一样。在另一种情况下,方法D、S、T应该并行执行。我在APIManager类中维护一个公共方法,它执行我的所有方法 我尝试在APIManager类中创建一个操作队列,但它不起作用。只有一个方法执行完毕,另一个方法执行才会发生。在这方面,有人能提出建议吗 -(void) m
-(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”);
}];
}
-(无效)方法B{
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”);
}];
}
{
}您尚未共享您正在实现的任何代码
NSOperationsQueue
只接受NSOperation
,您必须提供MaxConcurrentOperationCount
属性来告诉队列您要并行执行多少操作
在本例中,您定义了方法:methodA()、methodB()、methodC()。。。methodZ()。每个方法都使用
NSOperation
执行网络调用。但是您在NSOperationsQueue
中添加了哪些您没有提到的内容 尝试将NSOperationQueue实例上的maxConcurrentOperationCount设置为大于1的数量。如果没有解决,系统应该决定一次可以处理多少操作。@Greg,也尝试过这样做。尽管如此,操作仍在一个接一个地进行。这家伙很好地解释了NSO的操作,请仔细检查:我已经编辑了我的问题。请核实并提供您的意见。我已将MaxConcurrentOperationCount设置为100。我在我的init方法self.operationQueue=[[NSOperationQueue alloc]init]中在此之外声明;self.operationQueue.maxConcurrentOperationCount=100代码>
- (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;