欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

有关ios中循环引用问题的分析和解决

程序员文章站 2022-06-30 23:39:09
有关ios中循环引用问题的总结 如何干掉环 在此处不讲解循环引用是什么,请自行搜索。ios内存分为堆,栈,常量区,栈和常量区都是有管理的。 1.delegate与环 //classa: @...

有关ios中循环引用问题的总结


如何干掉环

在此处不讲解循环引用是什么,请自行搜索。ios内存分为堆,栈,常量区,栈和常量区都是有管理的。

1.delegate与环

//classa: 
@protocol clssadelegate  
- (void)fuck; 
@end 
@interface classa : uiviewcontroller 
@property (nonatomic, strong) id  delegate; 
@end 
//classb: 
@interface classb () 
@property (nonatomic, strong) classa *classa; 
@end 
@implementation classb 
- (void)viewdidload {  
   [super viewdidload];   
      self.classa.delegate = self;
   }

解决方法:如上代码,b强引用a,而a的delegate属性指向b,这里的delegate是用strong修饰的,所以a也会强引用b,这是一个典型的循环引用样例。而解决其的方式大家也都耳熟能详,即将delegate改为弱引用

2.block与环

@interface classa () 
@property (nonatomic, copy) dispatch_block_t block; 
@property (nonatomic, assign) nsinteger tem; 
@end 
@implementation classa 
- (void)viewdidload {    
  self.block = ^{       
  };  
 }

解决方法:

@interface classa ()

@property (nonatomic, copy) dispatch_block_t block;

@property (nonatomic, assign) nsinteger tem;

@end

@implementation classa

- (void)viewdidload {

__weak typeof(self) weakself = self

weakself.tem = 1;

};

}

3.weakself的缺陷

//classb是一个uiviewcontroller,假设从classa pushviewcontroller将classb展示出来 
@interface classb () 
@property (nonatomic, copy) dispatch_block_t block; 
@property (nonatomic, strong) nsstring *str; 
@end @implementation classb 
- (void)dealloc { } 
- (void)viewdidload {  
   [super viewdidload];   
   __weak typeof(self) weakself = self;  
            nslog(@"%@", weakself.str);    
  };   
 }

解决方案:

这里会有两种情况:

若从a push到b,10s之内没有pop回a的话,b中block会执行打印出来111。

若从a push到b,10s之内pop回a的话,b会立即执行dealloc,从而导致b中block打印出(null)。这种情况就是使用weakself的缺陷,可能会导致内存提前回收。

@interface classb ()

@property (nonatomic, copy) dispatch_block_t block;

@property (nonatomic, strong) nsstring *str;

@implementation classb

- (void)dealloc { }

- (void)viewdidload {

[super viewdidload];

__weak typeof(self) weakself = self;

self.block = ^{

__strong typeof(self) strongself = weakself; dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(10 * nsec_per_sec)), dispatch_get_main_queue(), ^{

});

};

self.block();

}

这么做和直接用self有什么区别,为什么不会有循环引用:外部的weakself是为了打破环,从而使得没有循环引用,而内部的strongself仅仅是个局部变量,存在栈中,会在block执行结束后回收,不会再造成循环引用。 这么做和使用weakself有什么区别:唯一的区别就是多了一个strongself,而这里的strongself会使classb的对象引用计数+1,使得classb pop到a的时候,并不会执行dealloc,因为引用计数还不为0,strongself仍持有classb,而在block执行完,局部的strongself才会回收,此时classb dealloc。

这种做法其实已经可以解决所有问题了,但是:block内部必须使用strongself,很麻烦,不如直接使用self简便。很容易在block内部不小心使用了self,这样子还会引起循环引用,这种错觉很难发现。

4.@weakify与@strongify

// 用法 
@interface classb () 
@property (nonatomic, copy) dispatch_block_t block; 
@property (nonatomic, strong) nsstring *str; 
@end 
@implementation classb 
- (void)dealloc { } 
- (void)viewdidload {     
[super viewdidload];     
self.str = @"111";     
@weakify(self)     
self.block = ^{         
@strongify(self)         dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(10 * nsec_per_sec)), dispatch_get_main_queue(), ^{             
nslog(@"%@", self.str);         
});     
};     
self.block();    
}