澄清弱引用和保留周期

编程入门 行业动态 更新时间:2024-10-11 07:29:42
本文介绍了澄清弱引用和保留周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest: request]; operationpletionBlock = ^{ if([operation hasAcceptableStatusCode]){ } };

ARC似乎不喜欢[operation hasAcceptableStatusCode],并且出现以下警告:在此块中强烈捕获'operation'可能会导致保留周期".

ARC doesn't seem to like [operation hasAcceptableStatusCode], and i get the following warning: "Capturing 'operation' strongly in this block is likely to lead to a retain cycle".

我对引用不是很有经验,知道要怎么走吗?

I'm not very experienced with referencing, any idea whats the way to go here?

谢谢, 推荐答案

块从外部捕获(保留)您引用的对象.

Blocks capture (retain) the objects that you reference from outside of them.

操作将保留completeBlock,它将保留操作,因此将保留循环.

operation will retain completionBlock which will retain operation, hence the retain cycle.

最好的办法是创建对该对象的弱引用,并将其传递给它

The best thing to do is create a weak reference to the object and pass that in instead

AFHTTPRequestOperation * __weak theOperation = operation operationpletionBlock = ^{ if (theOperation) { return; } };

弱引用在运行时是安全的,因此,如果已取消分配操作,则只会向nil发送一条消息.

Weak references are safe at runtime so if operation has been dealloced you will just send a message to nil.

更多推荐

澄清弱引用和保留周期

本文发布于:2023-11-08 06:40:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:周期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!