简体   繁体   English

在块中调用另一个引用self的方法的方法是否会导致保留周期?

[英]Does calling a method inside a block that calls another method referring to self cause a retain cycle?

Can doFirst cause a retain cycle here? doFirst可以在这里引起保留周期吗?

@interface Example : NSObject
@property (nonatomic, strong) void (^block)();
@end

@implementation Example

- (void)doFirst
{
    __weak id weakSelf = self;
    self.block = ^ {            
        [weakSelf doSecond];
    };

    self.block();
}

- (void)doSecond
{
    self.value = //...
    // do other stuff involving self
}
@end

Unlike blocks, methods are not objects; 与块不同,方法不是对象。 they cannot hold a permanent reference to objects. 它们不能持有对对象的永久引用。

Your code would not cause a retain cycle. 您的代码不会引起保留周期。 The fact that the code inside doSecond references self explicitly does not mean that self would get retained an extra time. doSecond内部的代码明确引用了self事实并不意味着self将获得额外的保留时间。 When your block calls doSecond , its self comes from the weakSelf reference inside doFirst . 当你的块调用doSecond ,其self来自weakSelf内部参考doFirst

Note: When you store blocks as properties, use (nonatomic, copy) instead of (nonatomic, strong) . 注意:将块存储为属性时,请使用(nonatomic, copy)代替(nonatomic, strong)

No It won't. ,不会。 Because It just point to method which won't hold whatwhere inside methods which just an reference as like object. 因为它仅指向方法,而该方法在像对象之类的引用内部将不保存任何内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM