简体   繁体   中英

Does iOS not clean up the completion block of UIView animation?

I experienced something odd with UIView animation blocks.

A view never deallocated after I run my animation code. Before calling animation code I set the view property to nil. Then inside the completion block I removed it from superview so after fade out it's gone.

But -dealloc of that faded out and removed view is never called. Then I thought maybe the completion block retains the view and made a weak reference using __weak which worked. Suddenly the view deallocated properly after animation completion.

But in the animation block itself I simply reference the view and set its alpha to 0. No __weak reference and still the view deallocates properly.

Conclusion: iOS cleans up the animation block after animation is done. But it does NOT clean up the completion block. Means: You must use __weak references for everything in the completion block because it sticks around forever.

Is there a magic way to trigger completion block disposal or cleanup so it releases all strong references? Of course I use ARC btw.

It sounds like you had inadvertently created a retain cycle by using a strong reference in your block. The view had a strong reference to the block, and the block had a strong reference to the view, and the mutual strong references prevented either one from ever being deallocated. Converting the block's reference to weak broke the cycle, allowing the view (and eventually the block) to be deallocated.

You'll find a good discussion of the possibility of creating retain cycles in block in this SO question: ARC, ivars in Blocks and Reference Cycles via Captured Self .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM