Recursive Block Retain Cycles

前端 未结 6 975
心在旅途
心在旅途 2020-12-13 04:44

Will this lead to any sort of retain cycle? Is it safe to use?

__block void (^myBlock)(int) = [^void (int i)
{
    if (i == 0)
        return;

    NSLog(@\"         


        
6条回答
  •  轮回少年
    2020-12-13 05:41

    If you are using ARC, you have a retain cycle, because __block object variables are retained by the block. So the block retains itself. You can avoid it by declaring myBlock as both __block and __weak.

    If you are using MRC, __block object variables are not retained, and you should have no problem. Just remember to release myBlock at the end.

提交回复
热议问题