When do I need to copy a block?

I do not know when I need to copy a block.
The documentation says when you expect the block to be used after destruction of the scope within which it was declared .

Do I need to copy it when I use them as callback methods? By copy, I mean creating a copy property for my block and saving the block there or sending it. The scope is likely to be destroyed when the download or the like ends. So what do I need to copy a block in this case? Can I create circular links this way?

Now I have a loader class (only for checking blocks, it is too dangerous for me to use it yet) that uses a completion block, and when I copy the block, the view controller that created the block and the loader class are not released.

Can you give me some good examples, especially using them to complete asynchronous operations such as downloading files?

Thanks.

+4
source share
1 answer

In most cases, methods that accept callbacks will copy a block to you

I can recall two cases when you need to copy a block yourself in Objective-C

  • when you return a block from a function
  • when you save the block of a class variable (this happens when you implement a method that accepts a callback at a later point).

Since this is the time when the block will fall out of scope before they are used or processed.

+6
source

Source: https://habr.com/ru/post/1391788/


All Articles