iphone - NSInvocationOperation 忽略 maxConcurrentOperationCount

标签 iphone ios nsoperationqueue twrequest nsinvocationoperation

我正在尝试使用 NSInvocationOperation 对一些 TWRequest 调用进行排队。它似乎以正确的顺序添加方法调用,但 doSomething: 方法几乎同时被调用,即并发运行,而不是一个接一个地调用,这正是我想要实现的。

此外,完整的顺序错误,这表明它不是一个接一个地运行...

- (void)prepare {

    if(!self.queue){
        self.queue = [[NSOperationQueue alloc] init];
        [self.queue setMaxConcurrentOperationCount:1];
    }

    for(NSString *text in calls){

        NSLog(@"Adding to Queue... %@", text);
        NSInvocationOperation *indexOperation = [[NSInvocationOperation alloc] initWithTarget:self  
                                                                                     selector:@selector(doSomething:) object:text];
        [self.queue addOperation:indexOperation];
    }     
}

- (void)doSomething:(NSString*)someText {

    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://something.com"] parameters:nil requestMethod:TWRequestMethodGET];
    NSLog(@"About to Perform Request... %@", someText);
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         dispatch_sync(dispatch_get_main_queue(), ^{
              // works fine
              NSLog(@"Network Finished... %@", someText);
         });
     }];
}

在日志中我看到了这个:

2011-12-30 18:34:34.553 app[32745:10703] Adding to Queue... 1
2011-12-30 18:34:34.555 app[32745:10703] Adding to Queue... 2
2011-12-30 18:34:34.556 app[32745:10703] Adding to Queue... 3
2011-12-30 18:34:34.557 app[32745:13e03] About to Perform Request... 1
2011-12-30 18:34:34.560 app[32745:13e03] About to Perform Request... 2
2011-12-30 18:34:34.563 app[32745:13e03] About to Perform Request... 3
2011-12-30 18:34:35.303 app[32745:10703] Network finished... 3
2011-12-30 18:34:35.454 app[32745:10703] Network finished... 2
2011-12-30 18:34:35.601 app[32745:10703] Network finished... 1

我期待在 (1) 完成后看到 (2) 执行请求等等...有任何指示吗?

最佳答案

操作队列工作正常。正如@Joe 在评论中所说,performRequestWithHandler: 启动异步连接并立即返回。您可以通过将 NSLog 添加到 doSomething 的末尾来查看这一点,如下所示:

- (void)doSomething:(NSString*)someText {
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://something.com"] parameters:nil requestMethod:TWRequestMethodGET];
    NSLog(@"About to Perform Request... %@", someText);
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         dispatch_sync(dispatch_get_main_queue(), ^{
              // works fine
              NSLog(@"Network Finished... %@", someText);
         });
     }];
    NSLog(@"doSomething Finished");
}

要让每个请求连续发生,您需要使请求在操作中同步(使用 signedRequest 方法和同步 NSURLConnection),或者不使用操作队列并调用下一个请求在当前请求的完成处理程序中。请记住,如果您使用操作队列,则操作执行的顺序不基于它们添加的顺序。您可能会考虑直接将 GCD 与串行调度队列一起使用。

关于iphone - NSInvocationOperation 忽略 maxConcurrentOperationCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8682904/

相关文章:

iphone - 在表格 View 的底部添加一个按钮

iOS:设备上的自定义位置

ios - 第一次更新事务,调用旧队列

ios - NSOperationOperationQueue cancelAllOperations 方法不会停止操作

ios - NSOperation 队列没有同时执行

android - 信用卡读卡器输出什么样的数据?

iphone - 当我将 NSDictionary 转换为 NSMutableDicitonary 时如何影响更改?

ios - 用于游戏开发的 RubyMotion

ios - Django 推送通知到 IOS

ios - 具有计时器的 UITableViewCell