对于iOS 6,如何在后台线程上加载数据然后更新UI? [英] For iOS 6, how do I load data on a background thread and then update the UI?

查看:104
本文介绍了对于iOS 6,如何在后台线程上加载数据然后更新UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多方法可以做到这一点,但IOS 6应用的当前,现代,正确的方式是什么?

There are lots of ways to do this, but what is the current, modern, correct way for IOS 6 apps?

以下是我现在正在做的事情:

Here's what I'm doing now:

- (void)viewDidLoad
{
    [super viewDidLoad];

    __weak MyViewController *weakSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                         (unsigned long)NULL), ^(void) {
        // Do work that will take a long time
        // Store data in member variables, etc.

        // Update the UI 
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.myTableView reloadData];
        });
    }
}

使用ARC,我不认为__weak参考是必要的,但我不是百分百肯定。

With ARC, I don't think the __weak reference is necessary, but I'm not 100% sure.

推荐答案

你正在做的事情是完全正确的方法!当你使用块并且完全是ARC时需要弱引用。在ARC之前,弱还不存在。

What you're doing is completely the right way to go! The weak reference is needed when you use blocks and is totally ARC. Before ARC, "weak" didn't exist yet.

这篇关于对于iOS 6,如何在后台线程上加载数据然后更新UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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