IOS两种常见的循环引用:
1,两个类之间互相定义对方的引用
如下:
//ARC code
@interface A : NSObject @property (nonatomic,strong) B* b; @end @interface B : NSObject @property (nonatomic,strong) A* a; @end
解决方法是,其中一个的属性用strong, 一个用weak,如下:
//ARC code
@interface A : NSObject @property (nonatomic,strong) B* b; @end @interface B : NSObject @property (nonatomic,weak) A* a; @end
2,Block中出现的循环引用
[self.tableView addPullToRefreshWithActionHandler:^{
self.isRefresh = YES; self.hideHud = YES; self.currentPage = 0; [self queryCarFault]; }];
如上,如果self要退出了,也就是dealloc,但是block还没有退出,self还在占用,导致dealloc也无法退出;
修改为:
__weak BBWarningRecordTableViewController* weakSelf = self;
[self.tableView addPullToRefreshWithActionHandler:^{
weakSelf.isRefresh = YES; weakSelf.hideHud = YES; weakSelf.currentPage = 0; [weakSelf queryCarFault]; }];