为什么我的代码会因为对此方法的一次调用而中断而不是另一次?

时间:2012-07-29 03:25:39

标签: macos cocoa nsurlconnection

我有一个Mac应用程序应该根据给定的用户名获取Twitter粉丝和朋友,然后反过来向Twitter询问每个返回的UserID的用户NAMES。正如你在下面看到的,我有一个方法可以调用Twitter API来获取朋友/关注者(工作正常)。是userNameForUserID:delegate:方法导致我出现问题。我过去常常同步执行这些请求,然后返回用户名的NSString。那条(现已注释掉的)总是破了,所以我尝试异步地使用NSURLConnection。仍然无法正常工作。我不明白为什么 [[NSString alloc] initWithContentsOfURL:...]适用于fetchFollowers ...方法,但是当我在其他方法中使用EXACT SAME方式时... ...

我在用于分配init的NSString以及URL内容的行上设置了一个断点,当我进入它时,它不会中断,返回,抛出异常,崩溃......没有。好像那条线刚刚被踩了一下(但是我的应用程序仍被阻止。

有什么想法吗?非常感谢!

NSString * const GET_FOLLOWERS = @"https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=";
NSString * const GET_FRIENDS = @"https://api.twitter.com/1/friends/ids.json?cursor=-1&screen_name=";
NSString * const GET_USER_INFO = @"https://api.twitter.com/1/users/show.json?user_id=";

@implementation TwitterAPI

+ (void)userNameForUserID:(NSNumber *)userID delegate:(id<UserNameDelegate>)delegate
{
    NSURL *url = [NSURL URLWithString:[GET_USER_INFO stringByAppendingString:[userID stringValue]]];

    //  NSString *JSON = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:req queue:queue completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error) {
        [delegate addUserNameToArray:data];
    }];
}

+ (NSArray *)fetchFollowersWithUserName:(NSString *)userName
{
    NSURL *url = [NSURL URLWithString:[GET_FOLLOWERS stringByAppendingString:userName]];

    NSArray *followerIDs;

    NSString *JSON = [[NSString alloc] initWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
    if ([JSON rangeOfString:@"error"].location == NSNotFound)
        followerIDs = [[JSON JSONValue] valueForKey:@"ids"];

    return followerIDs;

}

0 个答案:

没有答案