I'm trying to queue up some TWRequest calls using NSInvocationOperation. It seems to add the method calls in the correct order, but the doSomething: methods get called at the same time pretty much, ie run concurrently, rather than one after the other, which is what I want to achieve.
In addition, the complete in the wrong order, which suggests it's not running one after the other...
- (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);
});
}];
}
In the log I see this:
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
I'm expecting to see (2) to Perform Request after (1) has finished etc... Any pointers?
The operation queue is working fine. As @Joe said in the comment, performRequestWithHandler:
starts an asynchronous connection and returns immediately. You can see this by adding an NSLog
to the end of doSomething
as follows:
- (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");
}
To have each request happen serially you need to either make the request synchronous within the operation (use the signedRequest
method and a synchronous NSURLConnection) or don't use an operation queue and invoke the next request in the completion handler of the current request. Keep in mind that if you use an operation queue the order in which operations are performed is not based on the order they are added. You might consider using GCD directly with a serial dispatch queue.