缓慢的UI更新,添加微调器

时间:2014-03-07 15:08:33

标签: ios objective-c grand-central-dispatch segue

我已经创建了一个内部加载大量媒体的iPad应用程序,这可以将UI冻结几秒钟(特别是在较旧的iPad上)。我正在探索异步并添加一个微调器,但是我无法确定微调器在新的ViewController打开之前启动的正确位置。任何帮助都是适用的。

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

[activityIndicatorObject startAnimating];

dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
dispatch_async(downloadQueue, ^{

    understandingViewController *destViewController = segue.destinationViewController;
    destViewController.itemNumber = num1;
    destViewController.selectedItem = num1;

    dispatch_async(dispatch_get_main_queue(), ^{
        [activityIndicatorObject stopAnimating];
    });

});

}

2 个答案:

答案 0 :(得分:2)

在destinationViewController中使用ViewDidLoad添加activityIndi​​cator和startAnimating。

并在ViewDidLoad中,将数据加载到后台线程

这样的事情

 - (void)viewDidLoad {
   [super viewDidLoad];
    // add activity indicator
    // start animating

      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
    dispatch_async(queue, ^{
        // Perform non main thread operation
        // load data
        dispatch_sync(dispatch_get_main_queue(), ^{
            // perform main thread operation
        });
    });
}

答案 1 :(得分:0)

你听起来很困惑。视图控制器不是“在后台”。 CODE在后台运行。你不应该在prepareForSegue中进行异步调用。

我建议完全跳过对dispatch_async()的调用。相反,请查看使用NSURLConnection的sendAsynchronousRequest:queue:completionHandler:方法。这将允许您提交连接请求(传递队列参数的主队列)和连接完成后要执行的代码块。系统处理异步下载并在完成后通知您。

相关问题