Tuesday, 15 July 2014

ios - What's the best way to manage memory when a ivar inside a block might be released? -


Assume that you have something like this:

  ivarOutsideOfBlock = @ "foo"; [DoSomethingThatTakesAwhile start: ^ (NSError * error) {if (error!) {IvarOutsideOfBlock = @ "somethingElse"; // may crash because iVarOutsideOfBlock no longer exists}}];  

At this point, we say that in the context of ivar, the view controller becomes dealloacted. Will the code crash (INVALID ADDRESS)?

What is the best practice around this? Does it have to convert ivar into a strong asset on the View Controller?

The reference block inside is contained in the block viewController-> Ivar creates a strong reference to , and block Iwer owner before the block it will not be canceled.

It seems that you really have one because the owner of the block is similar to the referenced object. It is problematic: the blocks and other objects keep each other alive, and they can not be canceled. .


No comments:

Post a Comment