【发布时间】:2018-04-14 06:19:19
【问题描述】:
我的项目中的各个类中有几种方法,例如 methodA()、methodB()、methodC()...methodZ()。每个方法都使用 NSOperation 执行网络调用。在某些情况下,我必须并行执行方法,例如方法 A、D、M 应该并行执行。在另一种情况下,方法 D、S、T 应该并行执行。我在 APIManager 类中维护一个通用方法,它执行我的所有方法。
我尝试在 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”);
}];
}
-(void) 方法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”);
}];
}
- (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;
}
【问题讨论】:
-
尝试将 NSOperationQueue 实例上的 maxConcurrentOperationCount 设置为大于 1。如果未解决,系统应决定一次可以处理多少个操作。
-
@Greg,也尝试过这样做。行动还是在一个接一个地进行
-
这家伙已经很好地解释了 NSOperation,请通过这个:izeeshan.wordpress.com/2014/08/17/…
标签: ios objective-c nsoperation nsoperationqueue