Speaking of block. I want to talk about its basics.
Block is a closure function. The closure is
A function that references a free variable. This referenced free variable will exist with this function, even if it has left the environment where it was created.
In oC, block is created by default and allocated to the stack .. After the copy message is sent to it, it is moved to the stack. Prevent being released.
From the definition above, we can see that the block will keep all variables referenced by it.
What is the _ block parameter? What was done when modifying variables?
It is a storage type. This variable is not retained in the block. You can also modify (read/write) it in the block.
When to use _ weak/_ BlockMyViewController *
Weakself ??
The following two examples are analyzed.
1. Execute an animation.
[ UIView animateWithDuration: 0.5 animations: ^ { self.someView . alpha = 0 ; } completion: ^ ( BOOL finished ) { [ self.someView removeFromSuperView ]; }];
Block is not required.
- ( void ) fadeOutView {
[ UIView beginAnimations ]; [ UIView setAnimationDuration: 0.5 ]; [ UIView setAnimationDelegate: self ]; [ UIView setAnimationDidStopSelector: @selector ( animationDidStop:finished:context: )]; self.someView . alpha = 0 ; [ UIView commitAnimations ]; } - ( void ) animationDidStop: ( NSString * ) animationID finished: ( NSNumber * ) finished context: ( void * ) context { [self. someView removeFromSuperView ]; }
2. Use a notification.
-(Void) setupnotifications {[[nsicationcenter center defacenter center] addobserverfornotificationname: mywhizbangnotification object: Nil queue: [nsoperationqueue mainqueue] Block: ^ (nsnotification * notification) {// reload the table to display the new zhangbangs [self. tableview reloaddata];}-(void) dealloc {[[nsicationicationcenter defacenter center] removeobserver: Self]; [Super dealloc];}
Block is not used.
-(Void) setupconfigurications {[[nsicationcenter center defaultcenter] addobserver: Self selector: @ selector (onwhizbang:) Name: mywhizbangnotification object: Nil];}-(void) onwhizbang: (nsnotification *) Notification {// reload the table to display the new custom bangs [self. tableview reloaddata];}-(void) dealloc {[[nsicationcenter center defacenter center] removeobserver: Self]; [Super dealloc];}
There is no problem with the former and the latter.
To analyze the latter:
First, there are three objects.
Self.Nsnotificationcenter. Block.
Yes, block is also an object.
Nsicationicationcenter strongly references self.
Self strongly references block.
The block strongly references self.
Here
There is no problem with the nsicationicationcenter and self relationship.
When we are in dealloc of self
[[ NSNotificationCenter defaultCenter ] removeObserver: self ];
The block object will be released. Block releases the variable self that it references.
Why not in the first example?
Three objects
Self. someview. Block.
Self strong reference View
Strong block reference View
Here, block is strongly referenced by view owner self.
There is no circular reference here. When self is released, the block will release the view.
So for this situation. The solution is to prevent the block from strongly referencing self. Example: Use
_ Block myviewcontroller * weakself and _ weak myviewcontroller * weakself
The same effect can be achieved.
Reference: http://benscheirman.com/2012/01/careful-with-block-based-notification-handlers/
When to use _ weak/_ block myviewcontroller * weakself in a block