Do I need to use a weak self pointer if a method called from a Block uses self?

后端 未结 4 788
悲&欢浪女
悲&欢浪女 2021-01-15 14:46

Using self. in blocks causes retain cycles, so I need to create a reference to weakSelf. I understand this

BUT!

If from my block I

4条回答
  •  梦谈多话
    2021-01-15 15:19

    First of all: self does NOT cause a retain cycle. This is an urban legend. The incorrectness is obvious: A single reference can never cause a cycle. The usage of self inside a block causes a retain cycle, if the block is directly or indirectly referred by self, too, for example via a property.

    To your Q:

    If you "call" a method inside the block, the message probably has the receiver self, so you have a usage of self inside the block. For this reason it is captured and retained.

    If you really have no usage of self inside the block by neither using self nor using a property of self you do not have a usage of self and it is not captured, therefore not retained. But in this case you can have a dangling pointer or a nil'd reference.

提交回复
热议问题