Код TWRequest работает, но очень медленно показывает?
Я работаю с TWrequest для отображения моих списков твиттеров в виде таблицы. Следующий код работает. Проблема в том, что таблица очень медленно обновляется. Я делаю NSlogging ответ на запрос (который происходит очень быстро), я также перебираю каждый список и добавляю список 'name' в массив (который, опять же, происходит очень быстро <1 с). Но по какой-то необъяснимой причине обновление таблицы занимает примерно 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);
}
}
}];
}
}
}];
}
1 ответ
Извините, только что наткнулся на этот пост. Если вы еще не нашли решение, надеюсь, это поможет.
Я считаю, что executeRequestWithHandler может быть вызван в любом потоке, поэтому изменения пользовательского интерфейса следует отправлять в основной поток.
dispatch_async(dispatch_get_main_queue(), ^{
//update UI here
});
Или в случае перезагрузки данных таблицы вы можете использовать:
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];