1
我正在使用Twitter iOS 5 Framwork執行Twitter應用程序,並且它工作得很好。 我正在搜索設備上的所有可用TwitterAccounts,並將它們加載到tableview中以顯示它們。如果用戶觸摸了一個帳戶,則加載下一個視圖,並通過REST API請求所有追隨者,並將其加載到數組中以使用REST API分析每個名稱和照片。問題是,請求不是異步的,我首先得到一個nil數組,然後獲取API的響應。在執行異步請求時獲取nil數組的跟隨者返回
的requestMethod如下:
__block NSArray *responseArray;
NSURL *tweetURL = [NSURL URLWithString:@"https://api.twitter.com/1/followers/ids.json"];
NSString *username = [[[self getAvailableTwitterAccounts] objectAtIndex:user] username];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc]
initWithObjects:[NSArray arrayWithObjects:@"-1", username, @"1", nil]
forKeys:[NSArray arrayWithObjects:@"cursor", @"username", @"stringify_ids" ,nil]];
//Build request
TWRequest *getFollowerRequest = [[TWRequest alloc] initWithURL:tweetURL
parameters:parameters
requestMethod:TWRequestMethodGET];
[getFollowerRequest setAccount:[[self getAvailableTwitterAccounts] objectAtIndex:user]];
[getFollowerRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
NSError *jsonParsingError = nil;
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
responseArray = [response objectForKey:@"ids"];
NSLog(@"responseArray %@ for user: %@",responseArray,username);
});
}];
return responseArray;
}
和加載過程:
- (void)viewWillAppear:(BOOL)animated
{
followerIDArray = [[NSArray alloc] initWithArray:[[NBNTwitterConnect sharedTwitterConnect] getFollowerIDsForUser:userID]];
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self initDisplayArray];
}
-(void)initDisplayArray{
NSLog(@"followerIDArray: %@",followerIDArray);
[self performSelectorOnMainThread:@selector(arrayDidFinishedLoading:) withObject:nil waitUntilDone:NO];
}
-(void)arrayDidFinishedLoading:(NSArray *)array{
[self.tableView reloadData];
}
和輸出:
followerIDArray: (
)
2011-12-03 21:43:33.305 NBNTwitterChat[1858:10403] responseArray (
3840,
14064174,
281136865,
224987906
) for user:xyz
任何人都可以幫助我嗎?我搜索整個網絡,我需要使用TWRequest方法,而不是asihttprequest或其他東西。
謝謝,解決了這個問題。非常感謝。 – btype