使用 JSON 问题在 UITableView 中导航缓慢

Posted

技术标签:

【中文标题】使用 JSON 问题在 UITableView 中导航缓慢【英文标题】:Navigating in UITableView Slow with JSON Issue 【发布时间】:2013-07-31 01:31:44 【问题描述】:

我正在工作的表格视图中加载 4 个城市字符串,但是当我选择其中一个单元格并导航到另一个表格时,它导航速度太慢。我在具有不同链接的另一个表中使用下面的相同代码。你能告诉我为什么需要很长时间(~4 - 6 秒)才能到达另一个视图吗?

- (void)viewDidLoad

[super viewDidLoad];

NSURL * url  = [NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"];

NSData  * data = [NSData dataWithContentsOfURL:url];

NSError *e = nil;

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];

jsonArray1 = [[NSMutableArray alloc] init];

for(int i=0;i<[jsonArray count];i++)
                
    NSString  * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"];

    [jsonArray1 addObject:city];

-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

NSString *tempString = [jsonArray1 objectAtIndex:indexPath.row];
cell.textLabel.text = tempString;
return cell;

-

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

SeconfViewController *second = [[SeconfViewController alloc] initWithNibName:@"SeconfViewController" bundle:nil];

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

NSString *cellText = selectedCell.textLabel.text;

NSString *edit = [NSString stringWithFormat:@"http://kalkatawi.com/jsonTest.php?d=1&il=%@", cellText];

second.str2 = edit;

[self.navigationController pushViewController:second animated:YES];


【问题讨论】:

听起来你正在做的是在主线程上进行同步网络调用。查找进行异步网络调用的方法。有很多例子。 【参考方案1】:

在另一个屏幕上导航需要更多时间,因为您正在从服务器同步加载数据。在 ios 中,所有 UI 都是在主线程上完成的,通过在主线程上进行数据调用,你会阻塞它。我知道处理这个问题的最好方法是使用 GCD(Grand Central Dispatch)。它是 iOS 中的一个 API,可以毫不费力地为您生成线程。您只需要告诉您要调用以在后台线程上从服务器加载数据。当你这样做时,视图应该立即导航。您可以在数据到来时使用活动指示器。

dispatch_async(dataQueue, ^

        // Load all your data here

        dispatch_async(dispatch_get_main_queue(), ^
            // Update the UI

        );

    ); 

【讨论】:

【参考方案2】:

可能是因为您正在同步下载并且阻塞了主线程,这可能是您的应用冻结 4-6 秒的原因,请尝试异步下载您的 json

- (void)viewDidLoad

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
    NSData *response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"]];
    NSError *parseError = nil;
    jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];
    jsonArray1 = [[NSMutableArray alloc] init]
        for(int i=0;i<[jsonArray count];i++)
                        
          NSString  * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"];

          [jsonArray1 addObject:city];
        
    
    dispatch_sync(dispatch_get_main_queue(), ^
            [self.myTableView reloadData];
        );
);

【讨论】:

NSData 的initWithContentsOfURL: 不应用于访问remote 资源。文档对这一事实保持沉默,但在苹果官方开发者论坛上有相当多的苹果工程师声明initWithContentsOfURL: 系列方法只能用于文件访问。使用NSURLConnection 通过网络访问远程资源。 @CouchDeveloper @CarlosVela 现在效果更好,比以前更快。我已经提到过;首先它显示UITableView 然后加载数据是否有改善这个问题? @LuaiKalkatawi 尝试使用活动指示器来指示用户正在从您的网络服务下载数据。

以上是关于使用 JSON 问题在 UITableView 中导航缓慢的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 JSON 数据填充 UITableView?

如何在 UITableview 中使用 UISearchBar 在本地 JSON 中获取数据?

从 AFNetworking 2.0 加载带有 JSON 的 UITableView

iPhone 用 JSON 结果填充 UITableView(使用在 UISearchBar 中输入的搜索)

使用嵌套 JSON 填充 UITableView

Json数据在uitableview ios中没有更新?