[OC | Block] Block is a solution to circular references.
1. Ordinary circular reference
[Situation example]
Reasons for circular references:
A——>block——>A
Key points for solving circular references:
block does not directly strongly refer to A objects, and circular references can be resolved;
A——>block------>A
For some obvious circular references, Xcode will pop up a warning.
Running will not print out the message that classA is released.
[Scenario 1] One side uses weak references
@interface classA : NSObject
@property(nonatomic, copy)void (^myBlock)(void);
@end
@implementation classA
- (void)dealloc{
NSLog(@"%@释放了", [self class]);
}
@end
int main(int argc, const char * argv[]) {
classA* A = [classA new];
__weak typeof(A) weakA = A;
A.myBlock = ^(){
NSLog(@"%@",weakA);
};
A.myBlock();
return 0;
}
//结果
//2022-06-15 14:54:42.797527+0800 Block[59944:7377772] <classA: 0x100578920>
//2022-06-15 14:54:42.797921+0800 Block[59944:7377772] classA释放了
//Program ended with exit code: 0
[Scheme 2] Use the variable referenced inside the block as a formal parameter
@interface classB : NSObject
@property(nonatomic, copy)void (^myBlock)(classB*);
@end
@implementation classB
- (void)dealloc{
NSLog(@"%@释放了", [self class]);
}
@end
int main(int argc, const char * argv[]) {
{
classB* B = [classB new];
B.myBlock = ^(classB* b){
NSLog(@"%@",b);
};
B.myBlock(B);
}
while (YES) {
usleep(1000000);
}
return 0;
}
/*
2022-06-15 15:24:12.179634+0800 Block[62329:7428113] <classB: 0x100690960>
2022-06-15 15:24:12.180014+0800 Block[62329:7428113] classB释放了
*/
2. Circular references with delayed release
[Situation example]
Brief explanation: When the weakly referenced external variable is delayed in the block, if the external variable has been released, it will cause serious consequences. So it is necessary to add a strong reference to this external variable.
@interface classB : NSObject
@property(nonatomic, copy)void (^myBlock)(void);
@end
@implementation classB
- (void)dealloc{
NSLog(@"%@释放了", [self class]);
}
@end
int main(int argc, const char * argv[]) {
{
classB* B = [classB new];
__weak typeof(B) weakB = B;
B.myBlock = ^(){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"%@",weakB);
});
};
B.myBlock();
}
while (YES) {
usleep(1000000);
}
return 0;
}
/*
2022-06-15 15:06:40.133450+0800 Block[60991:7400895] classB释放了
2022-06-15 15:06:42.324550+0800 Block[60991:7400946] (null)
*/
Reasons for delayed release failure:
A——>block------>A. A is released, and the block still tries to operate A.
The key point to solve the delayed release release:
there is a strong pointer to the A object that can be freely manipulated, and the release can be delayed.
[Scheme 1] Strong and Weak Dance Together
@interface classB : NSObject
@property(nonatomic, copy)void (^myBlock)(void);
@end
@implementation classB
- (void)dealloc{
NSLog(@"%@释放了", [self class]);
}
@end
int main(int argc, const char * argv[]) {
{
classB* B = [classB new];
__weak typeof(B) weakB = B;
B.myBlock = ^(){
__strong typeof(weakB) strongB = weakB;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"%@",strongB);
});
};
B.myBlock();
}
while (YES) {
usleep(1000000);
}
return 0;
}
/*
2022-06-15 15:13:05.898034+0800 Block[61472:7409957] <classB: 0x10060fef0>
2022-06-15 15:13:05.899076+0800 Block[61472:7409957] classB释放了
*/
[Scheme 2] Create a controllable strong reference
@interface classB : NSObject
@property(nonatomic, copy)void (^myBlock)(void);
@end
@implementation classB
- (void)dealloc{
NSLog(@"%@释放了", [self class]);
}
@end
int main(int argc, const char * argv[]) {
{
classB* B = [classB new];
__block classB* C = B;
B.myBlock = ^(){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"%@",C);
C = nil;
});
};
B.myBlock();
}
while (YES) {
usleep(1000000);
}
return 0;
}
/*
2022-06-15 15:15:32.199761+0800 Block[61683:7414809] <classB: 0x100576e40>
2022-06-15 15:15:32.201276+0800 Block[61683:7414809] classB释放了
*/
[Scheme 3] Use the variable referenced inside the block as a formal parameter
@interface classB : NSObject
@property(nonatomic, copy)void (^myBlock)(classB*);
@end
@implementation classB
- (void)dealloc{
NSLog(@"%@释放了", [self class]);
}
@end
int main(int argc, const char * argv[]) {
{
classB* B = [classB new];
B.myBlock = ^(classB* b){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"%@",b);
});
};
B.myBlock(B);
}
while (YES) {
usleep(1000000);
}
return 0;
}
/*
2022-06-15 15:22:46.718545+0800 Block[62209:7425289] <classB: 0x1006c3ed0>
2022-06-15 15:22:46.720429+0800 Block[62209:7425289] classB释放了
*/