循环引用
来源:互联网 发布:淘宝批量下架宝贝 编辑:程序博客网 时间:2024/05/21 02:53
一、parent-child相互持有、委托模式
【案例】:
1
2
3
4
5
6
@interface
FTAppCenterMainViewController ()
{
}
@property
(weak,nonatomic) UITableView* myTableView;
@end
1
@property
(nonatomic, weak) id<ftactionsheetdelegate>delegate;</ftactionsheetdelegate>
【推荐方法】:
child只有parent的对象为weak类型:
1
@property
(nonatomic, weak) id<ftactionsheetdelegate>delegate;</ftactionsheetdelegate>
二、block
【案例】:
看下面的代码:
1
2
3
4
5
typedef
void
(^RequestNaviCallBack)(NSInteger naviCode,NSInteger httpCode,NSError * error);
@interface
FtNaviManager : NSObject
{
}
@property
(nonatomic, strong) RequestNaviCallBack naviCallBack;
【推荐方法】:
如果有循环引用,编译器会提示警告。
如果对象没有持有Block对象,那么不会产生循环引用。如果对象持有了block对象,那么在block引用self的时候这么定义:
1
__weak typeof(self) weakSelf = self;
三、NSTimer
【案例】:
1
2
3
4
5
6
@interface
FtKeepAlive : NSObject
{
NSTimer* _keepAliveTimer;
// 发送心跳timer
}
//实现文件
_keepAliveTimer = [NSTimer scheduledTimerWithTimeInterval:_expired target:self selector:
@selector
(keepLiveStart) userInfo:nil repeats:YES];
类持有了_keepAliveTimer,_keepAliveTimer又持有了self,造成循环引用。
【推荐方法】:
NSTimer会持有对象,所以:在删除对象之前,需要将timer的invalidate方法。
1
2
3
4
-(
void
)stopKeepAlive{
[_keepAliveTimer invalidate];
_keepAliveTimer = nil;
}
0 0
- 循环引用
- 循环引用
- 循环引用
- 循环引用
- 循环引用
- “循环引用”
- 循环引用
- 循环引用
- spring循环引用
- weak_ptr解除循环引用
- Block的循环引用
- 循环引用问题,
- 循环引用问题
- 循环引用的讨论
- 循环引用的处理
- block循环引用
- ios避免循环引用
- spring循环引用
- 走出地域局限性,我们能为客户带来什么
- opencv cvCanny算子以及与其他边缘检测算子的比较
- 二分查找
- 移动端跳转 防止误触
- 【肖凯】如何学习好R语言
- 循环引用
- class.getResource的用法
- LeetCode60:Permutation Sequence
- java新特性
- Pro Android学习笔记——开篇
- Django & Celery – Easy async task processing 翻译
- POJ 2013 Symmetric Order(水~)
- maven生成启动脚本
- 笔记116--DrawerLayout