避免Block中的强引用环
【避免Block中的强引用环】
In manual reference counting mode, __block id x; has the effect of not retaining x. In ARC mode, __block id x; defaults to retaining x (just like all other values). To get the manual reference counting mode behavior under ARC, you could use __unsafe_unretained __block id x;. As the name __unsafe_unretained implies, however, having a non-retained variable is dangerous (because it can dangle) and is therefore discouraged. Two better options are to either use __weak (if you don’t need to support iOS 4 or OS X v10.6), or set the __block value to nil to break the retain cycle.
例如,在MRC&ARC下,以下代码会存在强引用环:
MyViewController *myController = [[MyViewController alloc] init…];
// ...
myController.completionHandler = ^(NSInteger result) {
[myController dismissViewControllerAnimated:YES completion:nil];
};
在MRC下,解决上述强引用环的方法如下:
MyViewController * __block myController = [[MyViewController alloc] init…];
// ...
myController.completionHandler = ^(NSInteger result) {
[myController dismissViewControllerAnimated:YES completion:nil];
myController = nil;
};
在ARC下,解决上述强引用环的方法如下:
MyViewController *myController = [[MyViewController alloc] init…];
// ...
MyViewController * __weak weakMyViewController = myController;
myController.completionHandler = ^(NSInteger result) {
[weakMyViewController dismissViewControllerAnimated:YES completion:nil];
};
更好的方式是在使用__weak变量前,先用__strong变量把该值锁定,类似使用weak_ptr一样。
MyViewController *myController = [[MyViewController alloc] init…];
// ...
MyViewController * __weak weakMyController = myController;
myController.completionHandler = ^(NSInteger result) {
MyViewController *strongMyController = weakMyController;
if (strongMyController) {
// ...
[strongMyController dismissViewControllerAnimated:YES completion:nil];
// ...
}
else {
// Probably nothing...
}
};
避免Block中的强引用环的更多相关文章
- Block内的强引用
众所周知,当某个对象持有着一个Block的时候,如果在Block内部使用强引用反过来持有这个对象,就会导致引用循环.为了避免引用循环,可以使用__weak修饰符,苹果的官方文档在用代码演示__weak ...
- Java中的强引用,软引用,弱引用
作者:winterSunshine链接:https://www.zhihu.com/question/37401125/answer/100981172来源:知乎著作权归作者所有.商业转载请联系作者获 ...
- java中的强引用、软引用、弱引用、虚引用
1.强引用(Strong Reference):指程序代码中普遍存在的,类似“Object obj = new Object()”这类的引用,只要对象存在强引用关联,JVM必定不会回收这个对象: 2. ...
- java中的强引用(Strong reference),软引用(SoftReference),弱引用(WeakReference),虚引用(PhantomReference)
之前在看深入理解Java虚拟机一书中第一次接触相关名词,但是并不理解,只知道Object obj = new Object()类似这种操作的时候,obj就是强引用.强引用不会被gc回收直到gc roo ...
- Java中的强引用和弱引用
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/81114944 一.强引用 如下是强引用的经典 ...
- block中防止循环引用的一个高大上的宏定义
看惯了什么tempSelf weakSelf,来点高大的 #define weakify(...) \ rac_keywordify \ metamacro_foreach_cxt(rac_weaki ...
- Block中的循环引用警告
- block中如何避免循环引用
使用 weak–strong dance 技术 block 可以直接引用 self,但是要非常小心地在 block 中引用 self.因为在 block 引用 self,可能会导致循环引用.如下例所示 ...
- dispatch_async 的 block 中是否该使用_weak self
问题分析 我看过很多文章关于在dispatch_async的block里面使用_weak self, 但是让我疑惑的是,以下代码是否需要必须使用_weak self, 因为我也看到了很多观点说,在有些 ...
随机推荐
- JavaBean和Map的相互转换
JavaBean和Map的相互转换 一.JavaBean 1.什么是JavaBean? JavaBean其实就是一种遵循特定写法的类,必须遵循一定的规范: 类必须由public修饰,并且保证有公共的无 ...
- 结构化日志类库 ---- Serilog库
在过去的几年中,结构化日志已经大受欢迎.而Serilog是 .NET 中最著名的结构化日志类库 ,我们提供了这份的精简指南来帮助你快速了解并运用它. 0. 内容 设定目标 认识Serilog 事件和级 ...
- 爬取爱奇艺电视剧url
----因为需要顺序,所有就用串行了---- import requests from requests.exceptions import RequestException import re im ...
- 异步FIFO中空满信号如何产生?
异步FIFO中,空满信号该如何产生呢? 在复位的时候,读指针和写指针相等,读空信号有效(这里所说的指针其实就是读地址.写地址)当读指针赶上写指针的时候,写指针等于读指针意味着最后一个数据被读完,此时读 ...
- arm-linux-gcc配置安装
1.首先去下载arm-linux-gcc压缩包 https://pan.baidu.com/s/15QLL-mf0G5cEJbVP5OgZcA 密码:ygf3 2.把压缩包导入到Linux系统:htt ...
- Spark系列(二) Spark Shell各种操作及详细说明
并行化scala集合(Parallelize) //加载数据1~10 val num=sc.parallelize(1 to 10) //每个数据项乘以2,注意 _*2记为一个函数(fun) val ...
- jquery.raty.js 评星插件的使用
需要实现一个五星好评的功能,所以找到了这个JQ插件,使用起来还算简单,在这里记录下使用的方式. 第一步:导入这个插件和压缩包中的img文件夹 <script type="text/ja ...
- 给iOS开发新手送点福利,简述UISwitch的属性和用法
UISwitch属性 1. onTintColor 处于on时switch 的颜色 switchImage.onTintColor = [UIColor grayColor]; 2.tintC ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #14 虚拟存储子系统的调整
HACK #14 虚拟存储子系统的调整 本节介绍如何使用/proc进行虚拟存储子系统的调整.虚拟空间存储方式在Linux上向应用程序分配内存时,是通过以页面为单位的虚拟存储方式进行的.采用虚拟存储方式 ...
- Linux知识温习
进程间通信(IPC)介绍 mmap - 用户空间与内核空间 linux 进程地址空间的一步步探究 mmap 还是 shmget ? linux C/C++服务器后台开发面试题总结 Trie树详解及其应 ...