3

我正在使用 TWrequest 在表格视图中显示我的 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);
                        }

                    }
                }];

            } 
        } 
    }];

}
4

1 回答 1

5

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

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

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

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

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil    waitUntilDone:NO];
于 2013-02-01T20:21:54.453 回答