Sudhakar Tharigoppula
Sudhakar Tharigoppula

Reputation: 2867

NSOperationQueues are not executing concurrently

I am using NSOperationQueues in my application for downloading the data from the API and inserting into the local database. I am using one custom NSOpertion and adding to the NSOpertionQueue and executing for every request. When I perform two multiple requests with two different NSOperationQueues, they are not executing concurrently. I want know how to execute NSOperationQueues concurrently.

 //Request for master tables data.
  [RequestHandler getRequestForMasterTablesData:self];
  [RequestHandler postProspectListRequest:self];

/**
 * @brief ProspectList API: Gives the list of Prospects.
 */
+ (void)postProspectListRequest:(UIViewController*)lViewController {
    __weak ProspectListViewController *weakSelf = (ProspectListViewController*)lViewController;

    /**********************ONLINE MODE**********************/
    if ([self isNetworkAvailable]) {
        NSString *prospectListUrl = [NSString stringWithFormat:@"%@%@/%@",[[DataBaseManipulator sharedInstance] getCompanyURLFromUserDefaults],PROSPECTS,PROSPECTSLIST];
           RequestOperationManager *op = [[RequestOperationManager alloc] initWithGetRequest:prospectListUrl];
        __weak RequestOperationManager *weakOperation = op;
        op.completionBlock = ^{
            __strong RequestOperationManager *strong = weakOperation;

            dispatch_async(dispatch_get_main_queue(), ^{
                [[DataBaseManipulator sharedInstance] insertDataIntoProspectListDB:[strong.resultsDictionary valueForKey:@"prospects"]];
            });
        };
        NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];
        [networkQueue setMaxConcurrentOperationCount:1];
        [networkQueue addOperation:op];
    }
}

+ (void)getRequestForMasterTablesData:(UIViewController *)viewController {
if (![self isNetworkAvailable]) {
        [[NetworkMonitor instance]displayNetworkMonitorAlert];
        return;
    }
    NSString *masterTablesUrl = [NSString stringWithFormat:@"%@%@",[[DataBaseManipulator sharedInstance] getCompanyURLFromUserDefaults],MASTERTABLESDATA];
   __weak LoginViewController *weakSelf = (LoginViewController*)viewController;
    [[GenricUI instance] createLoadView];
    RequestOperationManager *op = [[RequestOperationManager alloc] initWithGetRequest:masterTablesUrl];
    __weak RequestOperationManager *weakOperation = op;
    op.completionBlock = ^{
        __strong RequestOperationManager *strong = weakOperation;
        dispatch_async(dispatch_get_main_queue(), ^{
            [[DataBaseManipulator sharedInstance] insertMasterDataIntoSalesItemTable:strong.resultsDictionary];
             [[GenricUI instance] removeLoadView];
            [weakSelf parseMasterTableResponse:strong.resultsDictionary];
        });
    };
    NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];
    [networkQueue setMaxConcurrentOperationCount:1];
    [networkQueue addOperation:op];
}

Iam calling the above two methods at a time but they are not executing concurrently. How to execute the above two methods concurrently.

Upvotes: 0

Views: 64

Answers (1)

gnasher729
gnasher729

Reputation: 52530

If you want concurrent execution, you shouldn't have a call

[networkQueue setMaxConcurrentOperationCount:1];

On the other hand, this code

NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];
[networkQueue setMaxConcurrentOperationCount:1];
[networkQueue addOperation:op];

is rather useless. You have a queue, add a single operation, and that's it. This doesn't achieve anything. If you want concurrent execution, create only one NSOperationQueue, keep hold of it to reuse it, and don't disable concurrent operations on it.

By the way, you seem to assume that once you have a network connection, a call to your server will succeed. That is to say the least rather optimistic.

Upvotes: 1

Related Questions