Retain cycle possibility with animateWithDuration:
Can I ever run into block retain cycle with animateWithDuration:animations:completion:
? If yes, then coul开发者_运维技巧d you please show me some example?
Also I couldn't find in Apple doc that completion:
block is copied to the heap. Can I assume that unless explicitly specified (like in addObserverForName:object:queue:usingBlock:
) the block is NOT copied to the heap?
There is no such thing as a block that is "copied to the stack". All blocks start on the stack; copying moves them to the heap. The block must be copied because it is called after your method returns (unless you do not animate anything or specify zero delay; in that case, the completion block seems to be called immediately).
There might be a case where you get a retain cycle: You have an infinitely-repeating animation and you pop the view controller, causing the view to lose its superview. In this case, I'd expect the animation to be cancelled instead of continuing indefinitely. This is not difficult to test.
Note that if the animation is cancelled, it presumably is also cancelled if you switch away from and back to a tab, or push a VC on top and then pop it.
I'd also try testing with a large duration/repeat count, in case UIKit special-cases infinitely-repeating animations to ignore the completion block (but I'm not sure it can do that, since I think the completion block has to be called if the animation is "interrupted" by setting one of the animated properties).
精彩评论