[OC | Block] Block 解决循环引用的方案。(持续更新)

一、普通循环引用

[情况示例]

循环引用的原因:
A——>block——>A
解决循环引用关键点:
block不直接强引用A对象,可解循环引用;
A——>block------>A

对于一些比较明显的循环引用,Xcode会弹出警告。
运行也不会打印出classA释放的消息。
循环引用警告

[方案1] 一方使用弱引用

@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

[方案2] 将block内部引用的变量作为形参

@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释放了
 */

二、延迟释放的循环引用

[情况示例]

简单说明:当block中延迟使用弱引用的外部变量时,如果外部变量已经释放了,会造成严重的后果。所以就要给这个外部变量加一个强引用。

@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)
*/

延迟释放失败的原因:
A——>block------>A。A释放,block中任然尝试操作A。
解决延迟释放释放的关键点:
有可自由操作的指向A对象的强指针,可延迟释放。

[方案1] 强弱共舞

@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释放了
 */

[方案2] 制造可控的强引用

@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释放了
 */

[方案3] 将block内部引用的变量作为形参

@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释放了
 */

猜你喜欢

转载自blog.csdn.net/qq_41749924/article/details/125295412
今日推荐