0

To download a bunch of Twitter timelines, I created TWRequest objects in a loop and put them into an NSOperationQueue:

twitterRequestQueue = [[NSOperationQueue alloc] init];

// Get a reference to a Twitter account

NSArray *screenNames = @[@"gruber", @"kottke", @"ev", @"brad", @"borkware", @"jack", @"greatdismal", @"wilshipley"];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];

for (NSString *screenName in screenNames) {
    NSDictionary *parameters = @{@"screen_name" : screenName, @"count" : @"200" };
    TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET];
    [request setAccount:account];

    // Make an operation using the Twitter request
    NSBlockOperation *twitterOperation = [NSBlockOperation blockOperationWithBlock:^{
        [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
            // Do stuff with the responseData
        }];
    }];

    // Put the requests into an operation queue
    [twitterRequestQueue addOperation:twitterOperation];
}

To determine when all timelines had been downloaded, I first tried using KVO on twitterRequestQueue's operationCount. I also tried adding a dependant operation to the queue after all the Twitter requests. Both these failed because the Twitter requests return almost immediately and get removed from the operation queue before their completion block gets called.

Instead, I stored my requests manually in a mutable array, calling a custom method [self requestCompleted:request]; at the end of the Twitter completion block, and manually removing the request from my mutable array as each finished:

- (void)requestCompleted:(TWRequest *)request
{
    NSDictionary *parameters = [request parameters];
    NSString *screenName = [parameters valueForKey:@"screen_name"];
    NSLog(@"Request completed: %@", screenName);

    [requestsInProgress removeObject:request];
    if ([requestsInProgress count] == 0) {
        NSLog(@"All requests finished");
    }
}

The other way I could make it work was to get the signedURLRequest from the Twitter request and download it synchronously using sendSynchronousRequest:returningResponse:error:.

Here's my question(s):

  • Is there any point in using an NSOperationQueue to download Twitter requests like this?
  • Is there a better technique to download multiple Twitter requests?
  • Could I improve the way I use an operation queue for this?
Community
  • 1
  • 1
nevan king
  • 112,709
  • 45
  • 203
  • 241
  • I think you should rely on a concurrent operation since in non-concurrent operations, when the `main` terminates, the op is removed from the queue. If you want, I could try to provide some hints to you. Have a nice day. – Lorenzo B May 15 '13 at 07:55

1 Answers1

0

Just use SLRequest and blocks. What do you want to achieve?

See for example https://github.com/ArchieGoodwin/NWTwitterHelper

nerowolfe
  • 4,787
  • 3
  • 20
  • 19