简体   繁体   English

避免“在此块中强烈捕获自我可能会导致保留周期”消息

[英]Avoiding the “capturing self strongly in this block is likely to lead to a retain cycle” message

every time I have to use a global var or property inside a block like this: 每次我必须在块中使用全局var或属性,如下所示:

self.save = ^(){
  if (isItSaving == NO) {
      [self saveMyFile];
  }
};

I have to rewrite this like 我必须重写这个

BOOL *iis = isItSaving;
id myself = self;

self.save = ^(){
  if (iis == NO) {
      [myself saveMyFile];
  }
};

or Xcode will complain "capturing self strongly in this block is likely to lead to a retain cycle... 或者Xcode会抱怨“在这个区块中强烈捕捉自我可能会导致保留周期......

It complains even about BOOL variables? 它甚至抱怨BOOL变量?

Redeclaring everything before a block appears to be a lame solution. 在块之前重新确定所有内容似乎是一个蹩脚的解决方案。

Is this the correctly way? 这是正确的方法吗? Is there an elegant way? 有优雅的方式吗?

This stuff is ugly. 这东西很难看。 I am using ARC. 我正在使用ARC。

The problem only occurs when referencing self from within the block, explicitly or implicitly. 只有在显式或隐式地从块内引用self时才会出现此问题。 There's no warning emitted when accessing global variables. 访问全局变量时没有发出警告。

In your case you probably accessed a (boolean) ivar. 在你的情况下,你可能访问了一个(布尔)ivar。 Accessing the ivar implicitly uses self , that's why the compiler is warning you (correctly) about a retain cycle. 访问ivar隐式使用self ,这就是编译器(正确地)警告你保留周期的原因。

The common way to fix the retain cycle is: 修复保留周期的常用方法是:

typeof(self) __weak weakSelf = self;

self.save = ^() {
    typeof(weakSelf) __strong strongSelf = weakSelf;
    if (strongSelf != nil && ! strongSelf->isItSaving) {
        [strongSelf saveMyFile];
    }
};

... and, yes, that's a bit of an ugly part of blocks. ......而且,是的,这是块的丑陋部分。

使用__unsafe_unretained typeof(self) weakSelf = self;

In addition to @NikolaiRuhe's response, in your example when declaring the properties 除了@ NikolaiRuhe的回复,在你的例子中声明属性

BOOL *iis = isItSaving;
id myself = self;

implies strong references, so use __weak self to prevent the retain cycle. 意味着strong引用,所以使用__weak self来阻止保留周期。 Then you might wonder why you need to declare a __strong reference to weak self within the block, and that's to make sure it doesn't get released during the life of the block, otherwise weakSelf->isItSaving would break if self was released. 然后你可能想知道为什么你需要在块中声明一个弱__strong self的__strong引用,这是为了确保它在块的生命期内不会被释放,否则如果self被释放, weakSelf->isItSaving将会中断。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这个区块中强烈捕获自我可能会导致保留周期 - capturing self strongly in this block is likely to lead to a retain cycle “使用可达性”“在此区块中强烈捕获'自我'很可能导致保留周期” - “Capturing 'self' strongly in this block is likely to lead to a retain cycle” using Reachability 如何在此块中强烈修复“捕获'块'可能会导致保留周期” - How to fix “Capturing 'block' strongly in this block is likely to lead to a retain cycle” iOS 5 Twitter Framework和completionHandler块 - “在这个块中强烈捕获'自我'可能会导致保留周期” - iOS 5 Twitter Framework & completionHandler block - “Capturing 'self' strongly in this block is likely to lead to a retain cycle” 在这种情况下,如何解决警告“在此块中强烈捕获‘自我’可能会导致保留周期”? - How to resolve warning "Capturing 'self' strongly in this block is likely to lead to a retain cycle" in this case? 警告:在此块中强烈捕获$ XXX可能会导致“ AFHTTPClient.m”中的保留周期 - Warning: Capturing $XXX strongly in this block is likely to lead to a retain cycle in 'AFHTTPClient.m' 块中的@synchronized(self)是否会导致保留周期? - Does @synchronized(self) in a block lead to a retain cycle? 强烈阻止捕捉“自我” - Block capturing 'self' strongly 捕获“自身”时,弱保留的块如何导致保留周期 - How can a weakly retained block cause a retain cycle when capturing “self” 缺少“可能导致保留周期”警告是否意味着将不会创建强参考周期? - Does the absence of a “likely to lead to a retain cycle” warning imply a strong reference cycle will not be created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM