我正在使用TWrequest在桌面视图中显示我的推特列表。以下代码有效。问题是更新表格非常慢。我正在对请求响应进行NSlogging(发生得非常快),我也循环遍历每个列表并将列表'name'添加到一个数组中(这也很快发生在< 1s)。但由于一些莫名其妙的原因,该表大约需要4秒左右才能更新。
为什么表重新加载需要这么长时间?问题是没有解析响应(因为我可以看到nslog这很快发生),它需要很长时间才能显示在表中?非常感谢帮助!
-(IBAction)getLists{
// First, we need to obtain the account instance for the user's Twitter account
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error) {
if (!granted) {
// The user rejected your request
NSLog(@"User rejected access to the account.");
}
else {
// Grab the available accounts
twitterAccounts = [store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) {
// Use the first account for simplicity
ACAccount *account = [twitterAccounts objectAtIndex:0];
// Now make an authenticated request to our endpoint
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
//[params setObject:@"1" forKey:@"include_entities"];
// The endpoint that we wish to call
NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1.1/lists/list.json"];
// Build the request with our parameter
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
// Attach the account object to this request
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!responseData) {
// inspect the contents of error
NSLog(@"error = %@", error);
}
else {
NSError *jsonError;
NSArray *timeline = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&jsonError];
if (timeline) {
// at this point, we have an object that we can parse
NSLog(@"timeline = %@", timeline);
for (NSDictionary *element in timeline) {
NSString *listName = [element valueForKey:@"name"];
[listsArray addObject:listName];
}
[listsTable reloadData];
}
else {
// inspect the contents of jsonError
NSLog(@"jsonerror = %@", jsonError);
}
}
}];
}
}
}];
}
答案 0 :(得分:5)
对不起,刚发现这篇文章。如果您还没有找到解决方案,希望这会有所帮助。
我相信可以在任何线程上调用performRequestWithHandler,因此应该将UI更改分派到主线程。
dispatch_async(dispatch_get_main_queue(), ^{
//update UI here
});
或者在重新加载表格数据的情况下,您可以使用:
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];