iOS开发之Block的循环引用
来源:互联网 发布:丰胸 知乎 编辑:程序博客网 时间:2024/05/22 15:27
循环引用的问题
让我们先来看一段代码。
1234
@interface MyClass ()@property (readonly) int val;@property (strong) dispatch_block_t work;@end
Capturing self strongly in this block is likely to lead to a retain cycle
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
@interface BlockTestView2 ()@property (readonly) int val;@property (nonatomic, strong) id obj;@property (strong) dispatch_block_t work;@end@implementation BlockTestView2- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _obj = @1; [self setup]; } return self;}- (void)setup { _val = 5; // retain cycle self.work = ^{ NSLog(@"BlockTestView2 %d", _val); };}// 1. Scoping// Avoid capturing self- (void)setup1 { int local = self.val; self.work = ^{ NSLog(@"BlockTestView2 %d", local); };}// 2. Programmatically// nil the block property- (void)setup2 {#pragma clang diagnostic push#pragma clang diagnostic ignored "-Warc-retain-cycles" self.work = ^{ NSLog(@"BlockTestView2 %d", _val); };#pragma clang diagnostic pop // nil the block property somewhere, please ignore // this call at this point [self cancel];}- (void)cancel { self.work = nil;}// 3. Attributes// Use __weak- (void)setup3 { __weak __typeof(self) weakSelf = self; self.work = ^{ __strong __typeof(self) strongSelf = weakSelf; NSLog(@"BlockTestView2 %@", strongSelf.obj); };}@end
0 0
- iOS开发之Block的循环引用
- iOS block之循环引用
- iOS开发之 成员变量 解决block循环引用方案
- iOS开发——Block循环引用问题的解决
- iOS开发——Block引起循环引用的解决方案
- iOS开发——Block引起循环引用的解决方案
- iOS开发-Block使用及循环引用的解决
- iOS开发--Block容易造成循环引用
- ios中block的循环引用
- iOS Block的循环引用问题
- IOS 如何避免Block的循环引用
- iOS中Block的循环引用问题
- iOS中block的循环引用问题
- iOS Block 中 循环引用的解决
- iOS开发内存优化之自动检测内存泄露,检查是否有循环引用,检查内存为何如此大,Block循环引用的检查
- iOS开发内存优化之自动检测内存泄露,检查是否有循环引用,检查内存为何如此大,Block循环引用的检查
- 精通iOS开发--第15章 Grand Central Dispatch和后台处理之Block与Self的循环引用
- swift之block的循环引用
- 第13周项目1-Prim算法的验证
- 初识Android系统平台
- 使用autotools工具用configure、make、make install编译安装linux工程的详细步骤
- Storm的Stream分组方式
- Idea Jsp <%%>中 request resopnse等无自动提示
- iOS开发之Block的循环引用
- [转载]Java面试题全集(下)
- javascript的运行机制
- HTMl中Meta标签详解以及meta property=og标签含义
- Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files de
- iOS 初级UI
- hive--mysql添加删除索引
- 表单元素的defaultValue属性
- node.js的事件驱动