我在我的項目中的各個類中有幾個方法,分別是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;
}
嘗試在NSOperationQueue實例上將maxConcurrentOperationCount設置爲大於1的數字。如果未解決,系統應該決定它可以一次處理多少個操作。 – Greg
@Greg,也試過這樣做。儘管如此,這些行動還是一個又一個的發生。 –
這個傢伙已經很好地解釋了NSOperation,請通過這個:https://izeeshan.wordpress.com/2014/08/17/multi-threading-using-nsoperation/ – manish