RAC uses a large number of blocks in applications. Because the Objective-C language memory management is based on reference count, to avoid the issue of circular reference, if you want to reference self in the block, use @ weakify (self) and @ strongify (self) to avoid strong references.
I. block circular references
- (void)loadView{ [super loadView]; _observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"testKey" object:nil queue:nil usingBlock:^(NSNotification *note) { [self dismissModalViewControllerAnimated:YES]; }];}- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:_observer];}
Code Analysis:
In the code above, we added an observer to the notification center and removed it when dealloc. Everything looks normal. But there are two problems:
The objects involved in this Code include: icationicationcenter, _ observer, block, and self.
A) The self object is referenced in the Message notification block, so the self object is blocked for retain. _ observer retain the block, and icationicationcentre of the notification center holds _ observer. Therefore, as long as the _ observer object has not been deregistered, the block will be held all the time, so that self will not be released, then dealloc will not be called. However, we hope that removeObserver will be removed from registration in dealloc to eliminate the retain of _ observer in the notificationcenter of the notification center.
Conclusion: icationicationcenter --> _ observer --> block --> self only releases _ observer when dealloc is called. It is clear that loop reference exists.
B) at the same time, _ observer defines a value in the class where self is located, so it is retain by self, which forms a circular reference.
Conclusion: self --> _ observer --> block --> self is also a circular reference.
Ii. Weak-Strong Dance
For the retain cycle in the block, there is a solution weak-strong dance in 2011 WWDC Session #322 (Objective-C Advancements in Depth), which is a pretty name. The implementation is as follows:
- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:_observer];}- (void)loadView{ [super loadView]; __weak TestViewController *wself = self; _observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"testKey" object:nil queue:nil usingBlock:^(NSNotification *note) { TestViewController *sself = wself; [sself dismissModalViewControllerAnimated:YES]; }];}
Before using self, use _ weak to modify self to create a weak self reference variable, before using self in a block, use _ strong to create a strong reference to the weak reference self to prevent the self from being released in advance.
In this way, the circular references can be broken.
Of course, __weak and _ strong are valid only in the case of ARC. For non-ARC, _ block is required. The effect is the same, as shown below:
- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:_observer]; [_observer release]; [super dealloc];}- (void)loadView{ [super loadView]; __block TestViewController *bself = self; _observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"testKey" object:nil queue:nil ngBlock:^(NSNotification *note) { [bself retain]; [bself dismissModalViewControllerAnimated:YES]; [bself release]; }];}
3. Weak-Strong Dance in ReactiveCocoa
For example:
@weakify(self); [RACObserve(self,photosArray) subscribeNext:^(id x){ @strongify(self); [self.collectionView reloadData];}];
RACObserver is a C macro that takes two parameters: an object and a key path on that object. it returns a signal whose values are sent whenever the key path's value changes. A completion value is sent when the object, in this case self, is deallocated. -->? We subscribe to this signal in order to reload our collection view whenever our photosArray property is changed.
Note: RACObserver is a macro definition with two parameters: an object and a key path on that object. When the object key path value changes, a signal is returned.
We subscribe to this signal. Once the photoArray attribute sends a change and returns signal, we can reload the collection view.
The weakify/strongify dance is all too common in Objective-C under ARC.Weakify creates a new, weak variable assigned to self. strongify then creates a new, strong variable in its scope assigned to the weak self. when strongify does this, it's using what's called a "shadow variable"-so named because the new, strong variable is called self, replacing the former strong reference to self.
Basically, the subscribeNext: block is going to capture self in its lexical scope, causing a reference cycle between self and the block. the block is stronugly referenced by the return value of subscribeNext:, a RACSubscriber instance. this is then captured by the RACObserver macro, which will be automatically deallocated once its first parameter, self is deallocated. without the weakify/strongify invocations, self wowould never be deallocated.
Analysis of possible block loop reference problems.
Self --> RACObserver macro --> RACSubscriber instance --> block --> self. If weakify/strongify is not used, loop references in reality will make self unable to be released.
Tip: when using the block, you should pay attention to the number of nested layers of the block. Improper misuse of multi-layer nested blocks may cause a disaster to the maintainability of the program.