一秒钟后在uitableview中加载数据.消除此延迟的任何替代方法 [英] Loading data after one second in uitableview. Any alternate way to eliminate this delay

查看:143
本文介绍了一秒钟后在uitableview中加载数据.消除此延迟的任何替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   NSURL *url=[NSURL URLWithString:string];
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.example.workQueue", DISPATCH_QUEUE_SERIAL);    
    dispatch_async(backgroundQueue, ^{
        NSData *data=[NSData dataWithContentsOfURL:url];
        NSDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        NSLog(@"%@",json);
        marray=[NSMutableArray array];
        for (NSDictionary *dict in json) {

        }
        dispatch_async(dispatch_get_main_queue(), ^{  
            [self.tableView reloadData];
        });
    });

这是处理数据并在Objective C中重新加载表的正确方法吗?如果是,那么在tableview上查看数据仍然会有些延迟.有什么办法可以消除这种延迟?顺便说一句,这是我的故事板中的第二个屏幕.

Is this the right way to handle data and reload the table in Objective C? If yes, then I still see some delay in seeing the data on tableview. Is there any way I can eliminate this delay? By the way this is second screen in my storyboard.

推荐答案

您正在做的很好. 只需在后台线程上下载数据并将其移至表视图以在主线程中重新加载数据即可.几乎可以肯定,将数据提供给表是您自己的延迟(网络延迟和解析时间),而不是UITabvleView处理reloadData调用时的延迟.

You are doing it all right. Download the data on background thread and handing it over to table view to reload data in main thread is all what you need to do. It's almost certainly your own delay (the network delay and the parsing time) in providing the data to the table, not the UITabvleView's delay in handling your reloadData call.

执行此操作时应遵循的一些一般规则:

Some of the general rules to be followed when doing this:

  1. 正在进行服务器调用时,在屏幕上显示加载叠加图.
  2. 返回数据后,将其传递到主线程上的表视图.立即删除加载叠加层.
  3. 不要在cellForRowAtIndexPath:方法中做繁琐的事情.
  1. Show loading overlay on screen when server call is going on.
  2. Once data is returned pass it on to table view on main thread. Remove the loading overlay now.
  3. Do not do heavy stuff in cellForRowAtIndexPath: method.

作为旁注,虽然同样重要,但是如果您遵循上述所有准则,请尝试以下内容.

As a side note, although the same thingy but try once with below if you are following all above guidelines.

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

这篇关于一秒钟后在uitableview中加载数据.消除此延迟的任何替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆