Why should we copy blocks rather than retain?

别来无恙 提交于 2019-12-10 17:36:07

问题


I recently met a problem using blocks with Facebook's app switching. I needed to call a block after the Facebook login.

First my block was destroyed when the app switched back ('cause it was on the stack), so I decided to retain it. But that didn't work, and I messed with that problem :/. I found a solution on that blog and also here.

My question is simply : why copy works and retain does not ?


回答1:


Because when you create a block there is nothing to retain, since it doesn't exist in the heap until you copy it there with Block_copy. This is covered in the WWDC lectures about blocks.

More info: http://www.friday.com/bbum/2009/08/29/blocks-tips-tricks/




回答2:


See my recent answer to another similar question:

By default blocks are created on the stack. Meaning they only exist in the scope they have been created in.
[...]
Read Stack and Heap Objects in Objective-C by Mike Ash for more info on stack vs. heap.



来源:https://stackoverflow.com/questions/14683746/why-should-we-copy-blocks-rather-than-retain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!