2015-10-19 75 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;' –