In this question,我問了一下下面的代碼,並保留週期:塊和弱引用保留週期
__weak Cell *weakSelf = self;
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
UIImage *image = /* render some image */
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[weakSelf setImageViewImage:image];
}];
}];
[self.renderQueue addOperation:op];
所有答案說明的是,這裏使用弱引用是沒有必要的,因爲這個代碼不結果保留週期。然而,儘管有一些更多的代碼實驗,下面就導致保留週期(如果我不使用弱引用,當前視圖控制器不釋放)
//__weak ViewController *weakSelf = self;
MBItem *close = [[MBItem alloc] initWithBlock:^{
[self dismissModalWithDefaultAnimation:NO];
}];
NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:close, nil];
[self.childObject setItems:items];
爲什麼會產生第二個結果一個保留週期,但不是第一個?
關鍵字是「保留CYCLE」。如同,我保留你,你保留我,所以誰先放手? – 2012-08-06 07:21:19