我的代码用于使用递归无法正常工作的Twitter好友 - iOS

Jas*_*lin 5 twitter recursion objective-c ios

我正在尝试使用游标浏览用户的推特朋友.由于你一次得到20个以及下一个光标,我想也许递归是处理这个问题的最佳方法.但是,我相信因为我使用的是完成处理程序块,所以它无法正常工作.我一直只得到两页朋友(40),然后又回来了.

- (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];
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

以及调用它的方法:

[self.twitterNetwork fetchTwitterFriendsForCrush:self.crush fromCursor:@"-1" usingManagedObjectContext:self.managedObjectContext withSender:self usingCompletionHandler:^{
    //
    [self reloadData];
}];
Run Code Online (Sandbox Code Playgroud)

更新:似乎我在每个请求上都收到相同的next_cursor数据.有没有人经历过这个?或者您是否在此代码中看到任何可能导致此问题的内容?

ner*_*lfe 0

我发现更复杂的方法更好。您可以使用https://api.twitter.com/1.1/friends/ids.json吗?获取您的好友 ID 列表。然后使用 1.1/users/lookup.json 您可以获得用户的完整信息。我写了一个小助手来使用 SLRequest 深入了解用户朋友 (#iOS6) https://github.com/ArchieGoodwin/NWTwitterHelper