刷新背景的UITableView的数据 [英] Reload data of UITableView in background

查看:67
本文介绍了刷新背景的UITableView的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我有一个的UITableViewController。

它的tableView在3个部分分成。

我下载DATAS每个从我的服务器的那些部分。要做到这一点,我有3个功能(例如F1 F2和F3)。每次更新相应的NSArray的,作为我的表的数据源。

现在我想要的是使用这种DATAS功能和刷新我的tableView一旦这3个功能都做了,但没有打扰用户重新载入。

我不跟异步请求,块,线等使用...我正在寻找的提示。

其实,这里是我做的:

   - (无效)viewDidLoad中
{
    //一些设置    [的NSTimer scheduledTimerWithTimeInterval:15.0目标:自我选择:@selector(reloadDatas)USERINFO:无重复:YES];    dispatch_queue_t队列= dispatch_get_main_queue();
    dispatch_async(队列,^ {
        [个体经营reloadDatas]
    });
} - (无效)reloadDatas
{
    dispatch_queue_t concurrentQueue = dispatch_get_main_queue();
    dispatch_async(concurrentQueue,^ {
        [自F1]
        [个体经营F2];
        [个体经营F3];
        [myDisplayedTable reloadData];
    });
} - (无效)F1
{
    //负荷DATAS一个URL请求和更新ARRAY1
}
- (无效)F2
{
    //负荷DATAS一个URL请求和更新数组2
}
- (无效)F3
{
    //负荷DATAS一个URL请求和更新ARRAY3
}

但在这里,我的tableView被冻结,直到它被刷新。

我不关心F1 F2和F3的执行顺序,但我需要等待这个3功能之前刷新我的tableView来完成。

感谢您的帮助。

修改

感谢您的答案。

下面是工作液:

由于mros suggets,我删除从viewDidLoad中调度队列,并在reloadDatas替换:

  dispatch_queue_t concurrentQueue = dispatch_get_main_queue();

  dispatch_queue_t mainThreadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

最后,我重装我表一个主线程

  dispatch_async(dispatch_get_main_queue()^ {[myDisplayedTable reloadData];});


解决方案

所以,你的后台线程实际上就是你的主线程。你必须使用dispatch_get_global_queue并指定一个优先真正得到一个不同的线程。此外,在viewDidLoad中调度异步是没用的,因为所有的视图控制器生命周期方法的调用在主线程。我会建议做一些在你的F1,F2和F3方法如下:

通过启动异步URL请求开始,然后在完成块,更新arrayX并重装tableview中的特定部分。这种方式,所有三个请求可以同时发生并且当每一个完成表中只更新必要的数据。或者,如果你只是想重装一次,只是一个后台线程代替你有concurrentQueue变量,然后在主线程上执行 [的tableView reloadData]

In my app, I have a UITableViewController.

Its tableView is divided in 3 sections.

I download datas for each of those sections from my server. To do this, I have 3 functions (for example f1 f2 and f3). Each updates a corresponding NSArray, used as data source for my table.

Now what I want is to reload datas using this functions and refresh my tableView once this 3 functions are done, but without disturbing the user.

I'm not used with asynchronous request, blocks, threads etc... and I'm looking for tips.

Actually, here is what I do :

-(void)viewDidLoad
{
    //some settings

    [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(reloadDatas) userInfo:nil repeats:YES];

    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        [self reloadDatas];
    });
}

-(void)reloadDatas
{
    dispatch_queue_t concurrentQueue = dispatch_get_main_queue();
    dispatch_async(concurrentQueue, ^{
        [self f1];
        [self f2];
        [self f3];
        [myDisplayedTable reloadData];
    });
}

-(void)f1
{
    //load datas with a url request and update array1
}
-(void)f2
{
    //load datas with a url request and update array2
}
-(void)f3
{
    //load datas with a url request and update array3
}

But here, my tableView is "frozen" until it is refreshed.

I don't care about the order of execution of f1 f2 and f3, but I need to wait for this 3 functions to be done before refresh my tableView.

Thanks for your help.

EDIT

Thanks for all your answers.

Here is the working solution :

As mros suggets, I removed the dispatch queue from the viewDidLoad, and replace in reloadDatas:

dispatch_queue_t concurrentQueue = dispatch_get_main_queue();

with

dispatch_queue_t mainThreadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

And finally, I reload my table in a main thread

dispatch_async(dispatch_get_main_queue(), ^{ [myDisplayedTable reloadData]; });

解决方案

So your "background thread" is actually your main thread. You have to use dispatch_get_global_queue and specify a priority to actually get a different thread. Also, the dispatch async in viewDidLoad is useless as all view controller lifecycle methods are called in the main thread. I would recommend doing something as follows in your f1, f2 and f3 methods:

Start by launching an asynchronous url request, then in the completion block, update arrayX and reload a particular section of your tableview. This way all three requests can happen simultaneously and the table just updates the necessary data when each one finishes. Alternatively, if you only want to reload once, just replace the concurrentQueue variable you have with a background thread and then perform [tableView reloadData] on the main thread.

这篇关于刷新背景的UITableView的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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