gpt4 book ai didi

xcode - TWRequest 代码有效但显示速度很慢?

转载 作者:行者123 更新时间:2023-12-01 09:38:39 25 4
gpt4 key购买 nike

我正在使用 TWrequest 在表格 View 中显示我的 Twitter 列表。以下代码有效。问题是更新表格非常慢。我正在 NSlogging 请求响应(发生得非常快),我也在遍历每个列表并将列表“名称”添加到数组(同样,发生得非常快 <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);
}

}
}];

}
}
}];

}

最佳答案

抱歉,刚看到这篇文章。如果您还没有找到解决方案,希望这会有所帮助。

我相信 performRequestWithHandler 可以在任何线程上调用,因此应该将 UI 更改分派(dispatch)到主线程。

dispatch_async(dispatch_get_main_queue(), ^{
//update UI here
});

或者在重新加载表格数据的情况下,您可以使用:

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil    waitUntilDone:NO];

关于xcode - TWRequest 代码有效但显示速度很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13652005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com