NSOperationQueues不是同时执行的

时间:2015-02-26 09:56:13

标签: ios nsoperation nsoperationqueue

我在我的应用程序中使用NSOperationQueues从API下载数据并插入本地数据库。我正在使用一个自定义NSOpertion并添加到NSOpertionQueue并为每个请求执行。当我使用两个不同的NSOperationQueues执行两个多个请求时,它们不会同时执行。我想知道如何同时执行NSOperationQueues

 //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一次调用上述两种方法,但它们不是同时执行的。如何同时执行上述两种方法。

1 个答案:

答案 0 :(得分:1)

如果你想要并发执行,你就不应该打电话

[networkQueue setMaxConcurrentOperationCount:1];

另一方面,这段代码

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

相当无用。你有一个队列,添加一个操作,就是这样。这不会实现任何。如果您想要并发执行,只需创建一个 NSOperationQueue,保持它以重用它,并且不要在其上禁用并发操作。

顺便说一句,您似乎假设一旦建立了网络连接,就会成功调用您的服务器。也就是说至少相当乐观。