@strongify是什么原因 [英] What is the reason of @strongify

查看:118
本文介绍了@strongify是什么原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ReactiveCocoa 中,有一个宏可以阻止保留周期 @weakify 和@strongify。根据我的理解 @weakify 做一些像我通常做的那样的创建 __弱在块中使用的参考,但是怎么样 @strongify

In ReactiveCocoa there is macro to prevent retain cycle @weakify and @strongify. From my understanding @weakify do something like what I usually do that is create __weak reference for using in the block, but what about @strongify?

为什么我需要在街区再次强大?

Why do I need to make it strong again in the block?

以下是一些示例用法:

@weakify(self);
[RACObserve(self, username) subscribeNext:^(NSString *username) {
    @strongify(self);
    [self validateUsername];
}];


推荐答案

如果您只是在块中使用弱引用,在执行块时,self可以被释放。但是如果你想确保自己保持在内存中直到块完成执行,你必须将弱引用转换回强。

If you just use a weak reference within the block, self can get deallocated while the block is being executed. But if you want to ensure that self stays in memory until the block finishes execution, you have to convert the weak reference back to strong.

这篇关于@strongify是什么原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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