Suneel G
Suneel G

Reputation: 40

Execute multiple NSOperations in parallel

I have several methods in various classes in my project say methodA(), methodB(), methodC()... methodZ(). Each method executes network call using NSOperation. There are cases where I have to execute the methods in parallel like methods A, D, M should execute in parallel. Say in another case methods D, S, T should execute in parallel. I maintain a common method in APIManager class which executes all my methods.

I tried creating a operation queue in APIManager class but its not working. Only once a method execution is done, another method execution is happening. Can anyone suggest in this regard?

-(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) 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;

}

Upvotes: 0

Views: 651

Answers (1)

NSPratik
NSPratik

Reputation: 4846

You have not shared any code what you are implementing.

NSOperationsQueue only accepts NSOperation and you will have to provide MaxConcurrentOperationCount property to tell the queue how many operations you want to execute parallelly.

In your case, you defined methods: methodA(), methodB(), methodC()... methodZ(). Each method executes network call using NSOperation. But what are you adding in NSOperationsQueue, that you haven't mentioned.

Upvotes: 0

Related Questions