Iphone NSInvocationOperation忽略maxConcurrentOperationCount

Iphone NSInvocationOperation忽略maxConcurrentOperationCount,iphone,ios,nsoperationqueue,twrequest,nsinvocationoperation,Iphone,Ios,Nsoperationqueue,Twrequest,Nsinvocationoperation,我正在尝试使用NSInvocationOperation对一些TWRequest呼叫进行排队。它似乎以正确的顺序添加了方法调用,但是doSomething:methods几乎是同时被调用的,即并发运行,而不是一个接一个地运行,这就是我想要实现的 此外,完成的顺序错误,这表明它没有一个接一个地运行 - (void)prepare { if(!self.queue){ self.queue = [[NSOperationQueue alloc] init];

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

此外,完成的顺序错误,这表明它没有一个接一个地运行

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

我希望看到(2)在(1)完成后执行请求等。。。有指针吗?

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

- (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。

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

- (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。

不能根据异步网络调用的结果说它不工作。不能说它是基于异步网络调用的结果而工作的。谢谢各位…我选择在完成处理程序中调用下一个请求。谢谢大家。。。我选择在完成处理程序中调用下一个请求。