在这个问题中,我询问了以下代码并保留了周期:
__weak Cell *weakSelf = self;
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
UIImage *image = /* render some image */
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[weakSelf setImageViewImage:image];
}];
}];
[self.renderQueue addOperation:op];
Run Code Online (Sandbox Code Playgroud)
所有答案都指出在这里使用弱引用不是必需的,因为此代码不会导致保留周期.但是,在尝试更多代码时,以下操作会导致保留周期(如果我不使用弱引用,则不会释放当前视图控制器)
//__weak ViewController *weakSelf = self;
MBItem *close = [[MBItem alloc] initWithBlock:^{
[self dismissModalWithDefaultAnimation:NO];
}];
NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:close, nil];
[self.childObject setItems:items];
Run Code Online (Sandbox Code Playgroud)
为什么第二个会导致保留周期而不是第一个?