5

I'm trying to page through a user's twitter friends using cursors. Since you get 20 at a time along with a next cursor, I thought perhaps recursion was the best way to handle this. However, I believe because I'm using completion handler blocks, it isn't working correctly. I keep getting just two pages of friends (40), and it returns.

- (void)fetchTwitterFriendsForCrush:(Crush*)crush
                         fromCursor:(NSString*)cursor
          usingManagedObjectContext:(NSManagedObjectContext*)moc
                         withSender:(id) sender
             usingCompletionHandler:(void(^)())completionHandler
{
    // twitter returns "0" when there are no more pages to receive
    if (([cursor isEqualToString:@"0"]) || (cursor == nil)) {
        completionHandler();
        return;
    }
    NSString *urlString =
    [NSString stringWithFormat:@"https://api.twitter.com/1.1/friends/list.json?cursor%@skip_status=1", cursor];
    NSURL *requestURL = [NSURL URLWithString:urlString];
    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                            requestMethod:SLRequestMethodGET
                                                      URL:requestURL
                                               parameters:nil];
    request.account = self.twitterAccount;
    [request performRequestWithHandler:
     ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if (error) {
            NSLog(@"Error getting twitter friends = %@", error);
        }
        if (responseData) {
            NSError *jsonError;
            NSString *nextCursor = nil;
            NSMutableArray *friendsArray = [NSMutableArray arrayWithCapacity:100];
            NSDictionary *friendsDictionary =
            [NSJSONSerialization JSONObjectWithData:responseData
                                            options:0
                                              error:&jsonError];
            if ([friendsDictionary valueForKey:@"next_cursor_str"]) {
                nextCursor = [friendsDictionary valueForKey:@"next_cursor_str"];
            }
            if ([friendsDictionary valueForKey:@"users"]) {
                [friendsArray addObjectsFromArray:[friendsDictionary valueForKey:@"users"]];
            }
            for (NSDictionary *singleFriend in friendsArray) {
                NSString *twitterID = [singleFriend valueForKey:@"id_str"];
                NSString *name = [singleFriend valueForKey:@"name"];
                NSString *screenName = [singleFriend valueForKey:@"screen_name"];
                dispatch_queue_t mainQueue = dispatch_get_main_queue();

                dispatch_async(mainQueue, ^(void) {
                    // update model
                    TwitterFriend *newTwitterFriend =
                    [TwitterFriend twitterFriendWithTwitterID:twitterID
                                                     forCrush:crush
                                    usingManagedObjectContext:moc];
                    newTwitterFriend.name = name;
                    newTwitterFriend.screenName = screenName;
                });
            }
            [self fetchTwitterFriendsForCrush:crush
                                   fromCursor:nextCursor
                    usingManagedObjectContext:moc
                                   withSender:self
                       usingCompletionHandler:nil];
        }
    }];
}

And the method that calls it:

[self.twitterNetwork fetchTwitterFriendsForCrush:self.crush fromCursor:@"-1" usingManagedObjectContext:self.managedObjectContext withSender:self usingCompletionHandler:^{
    //
    [self reloadData];
}];

UPDATE: It appears that I'm receiving the same next_cursor data on every request. Has anyone experienced this? Or do you see anything in this code that would cause that?

Jason C. Howlin
  • 3,858
  • 3
  • 21
  • 29

1 Answers1

0

I found that a more complex way is better. You may use https://api.twitter.com/1.1/friends/ids.json? to get your friends ids list. Then using 1.1/users/lookup.json you may get the full info for users. I wrote a small helper to drill down user friends with SLRequest (#iOS6) https://github.com/ArchieGoodwin/NWTwitterHelper

nerowolfe
  • 4,787
  • 3
  • 20
  • 19