简体   繁体   中英

When is reference to self in block a retain cycle?

I've got a problem that is probably fairly common. In an ARC environment, a block that needs to reference self will outlive self. I'm aware of the convention to use __weak typeof(self) weakSelf = self; in blocks to avoid retain cycles. The problem is that we do need to retain self. So my question is: When does a reference to self in a block create a retain cycle? This post post suggests that in animation blocks, reference to self is ok.

I had also considered a solution like the following, but am not sure it changes anything:

__block typeof(self) blockSelf = self;

[someObj someMethodTakingCallback:^{
    //do some stuff
    blockSelf = nil;
 }];  

如果self除了保留`self之外,还保留了块,您将获得一个参考周期。

In this case you would have a retain cycle if self holds a reference to someObj since someObj is probably holding a reference to self. It's actually a retain cycle if there's any retaining reference path from self to someObj.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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