斯威夫特关闭[弱个体经营]和异步任务 [英] Swift closures [weak self] and async tasks

查看:95
本文介绍了斯威夫特关闭[弱个体经营]和异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试想一个情况,当你想为异步从服务器中加载一些文字并将结果显示在 的ViewController的 的UITextField

Imagine a situation, when you want to asynchronously load some text from the server and display the result in the ViewController's UITextField.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

    //... some long running async operation

    if let textResponse = responseFromServer {
        dispatch_async(dispatch_get_main_queue(), { [weak self] () in
            self?.textField.text = textResponse
        })
    }
})

答)我是否需要使用异步调用关闭中使用[薄弱的自我]?

我想我需要的,但我不知道我以后在这里读计算器一些Q / A,通过不使用[弱个体经营]为异步任务+倒闭了不少开源的应用程序去了。

I thought I need to, but I am not sure after I read some Q/A here at StackOverflow and went through quite a few open source apps that don't use [weak self] for async tasks + closures.

即:

唯一的一次,你真的想用[无主的自我]或[弱
  个体经营]是当你将创建一个有力的参考周期。
  (<一href=\"http://stackoverflow.com/questions/24320347/shall-we-always-use-unowned-self-inside-closure-in-swift\">Shall我们总是用斯威夫特 [无主的自我]封内)

The only time where you really want to use [unowned self] or [weak self] is when you would create a strong reference cycle. (Shall we always use [unowned self] inside closure in Swift)

有在我的情况下,没有很强的参考周期。

There is no strong reference cycle in my case.

但要清楚,它仍然是最好用的一个有力的参考
  这种情况。 (斯威夫特ARC和块的)

But to be clear, it would still be best to use a strong reference in this circumstance. (Swift ARC and blocks)

乙。)比方说,这是很好的去与强大的参考。 什么,当用户导航到不同的页面异步加载中间发生到ViewController?难道保持无形的ViewController在应用程序内存中,直到异步任务完成?

B.) Let's say it's good to go with the strong reference. What happens to the ViewController when the user navigates to the different page in the middle of async loading? Would it keep the invisible ViewController in the app memory until the async task finishes?

推荐答案

有没有强有力的参考周期(保留周期)在这里。如果您使用的强引用,它只要调度运行块解决。如果你需要,你理论上可以在这里使用很强的借鉴意义。

There is no strong reference cycle (retain cycle) here. If you employ a strong reference to self, it is resolved as soon as the dispatch block runs. You theoretically could use strong reference here if you needed to.

说了这么多,我会用在这种情况下弱引用建议。有一个在完全保持更新的文本字段对于已经被开除视图的宗旨,为耗时的过程期间很强的借鉴意义没有意义的。如果您在更新其他模型对象或类似的,也许你会需要保持有力的参考,但你并不需要在这种情况下这样做。作为一般原则,应该尽快释放内存在合理可行的。

Having said that, I would advise using a weak reference in this case. There's no point in maintaining a strong reference for the duration of the time consuming process solely for the purpose of updating a text field for a view that has already been dismissed. If you were updating other model objects or the like, perhaps you might need to keep the strong reference, but you don't need to do so in this case. As a general principle, one should release memory as soon as reasonably possible.

更妙的是,我也看长时间运行的异步操作,并决定我是否真的希望它继续运行视图控制器已经被解雇了。如果没有,我会倾向于也使请求取消,然后让 DEINIT 取消请求。而且,在这种情况下,你肯定会想使用弱引用(或者 DEINIT 不会被调用,直到长时间运行的异步操作完成)。

Even better, I'd also look at the "long running async operation" and decide whether I really want it to continue to run after the view controller has been dismissed. If not, I'd be inclined to also make the request cancelable and then have deinit cancel the request. And, in that case, you would definitely want to use weak reference (or else deinit wouldn't be called until the long running async operation finishes).

这篇关于斯威夫特关闭[弱个体经营]和异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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