YYKit/UtilityiOS面试题iOS

YYWeaKProxy的学习

2017-09-27  本文已影响99人  _阿南_
图片来之网络

查看了YYWeakProxy的代码,仅仅是重载了父类的方法,将输入的target保存为实例变量,然后返回self。在YYKit中的使用地方有:
YYAnimatedImageView中151行

_link = [CADisplayLink displayLinkWithTarget:[YYWeakProxy proxyWithTarget:self] selector:@selector(step:)];

YYWebImageOperation.m的306行

_connection = [[NSURLConnection alloc] initWithRequest:_request delegate:[YYWeakProxy proxyWithTarget:self]];

YYLabel.m的143行:

_longPressTimer = [NSTimer timerWithTimeInterval:kLongPressMinimumDuration
                                              target:[YYWeakProxy proxyWithTarget:self]
                                            selector:@selector(_trackDidLongPress)
                                            userInfo:nil
                                             repeats:NO];

等等。

主要是使用在传入self时,方法会对self进行强引用。使用YYWeakProxy是为了解决强引用的问题。
强引用引起的问题是,因为一般NSTimer的invalidate方法是放在dealloc方法中去执行,而RunLoop已经强引用了NSTimer,所以当界面已经被摧毁时,而因为self被NSTimer强引用,NSTimer被RunLoop引用,所以不会执行dealloc方法。因为不执行dealloc方法,导致invalidate不会被执行。此时self就会内存泄露了。
使用了YYWeakProxy后,当界面被摧毁时,因为NSTimer没有引用self,而是引用了YYWeakProxy。所以可以执行dealloc方法,此时在dealloc中执行了invalidate。这样就会解除NSTimer对YYWeakProxy的强引用。最终self,NSTimer和YYWeakProxy也都得到了释放,内存没有被泄露。
感谢@席萍萍_iOS深圳的指点。

// END

上一篇 下一篇

猜你喜欢

热点阅读