我在块中有许多方法,在下一个内部触发一个方法,以便将某些数据与Web服务同步。其中大多数都表现得很好,但有一种方法不会让我在调用self
后提及capturing self strongly in this block is likely to lead to a retain cycle
警告。
这就是我的意思:
[self deleteEntriesCorrespondingToDeletedNotesInNotebook:notebook success:^{
[self deleteNotesToMatchDeletedEntriesWithCompletion:^{
[self deleteResourcesToMatchDeletedMediaItemsWithCompletion:^{
[self addOrUpdateEntriesCorrespondingToUpdatedNotesInNotebook:notebook success:^{
//Anything calling a property or self after this point is a problem and gives the warning
[self addOrUpdateNotesCorrespondingToUpdatedEntriesWithCompletion:^{
}];
}failure:^{
}];
}];
}];
}failure:^{
}];
为什么只有物品通过这一点的任何想法都有问题?如果我用其他类似方法替换之前的方法,则没有问题。问题仅在使用addOrUpdateEntriesCorrespondingToUpdatedNotesInNotebook:
后才存在。
答案 0 :(得分:3)
[self deleteEntriesCorrespondingToDeletedNotesInNotebook:notebook success:^{
[self deleteNotesToMatchDeletedEntriesWithCompletion:^{ //this line here and the rest in your downward loop
不要使用自我。而是在第一行之前执行此操作
__typeof__(self) __weak _weakSelf = self;
然后从第二行开始,将self
替换为weakSelf
试试这个。欢呼声
答案 1 :(得分:2)
您的方法的所有都可以“表现良好”或创建保留周期,具体取决于它们的含义 完成块。
如下所述:Blocks retain cycle from naming convention?,clang编译器使用命名约定来决定是否发出
是否警告:所有方法add...
和set...
(但不是addOperationWithBlock
!)
引起警告,其他方法没有。