私は、特定のユーザー名でTwitterのフォロワーや友達を取得し、返された各ユーザーIDのユーザー名をTwitterに要求することになっているMacアプリケーションを持っています。以下に示すように、Twitter APIを呼び出して友達/フォロワーを取得するメソッドがあります(これは正常に機能します)。問題を引き起こしているのはuserNameForUserID:delegate:メソッドです。以前はこれらのリクエストを同期的に実行し、その時点でユーザー名のNSStringを返していました。その(現在コメントアウトされている)行は常に壊れていたので、NSURLConnectionを使用して非同期で実行してみました。それでも動作しません。[[NSString alloc] initWithContentsOfURL:...]がfetchFollowers ...メソッドで機能する理由がわかりませんが、他の方法でまったく同じように機能する場合はわかりません...
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;
}