关于block中内存释放

来源:互联网 发布:电子线路板设计软件 编辑:程序博客网 时间:2024/06/05 17:44

block里面不能直接用self,需转为weak弱类型
写法1:
__weak typeof(self) weakSelf = self;//转为weak弱类型

[self.network doRequestUsingCookieWithType:TLDRequestType_Get urlString:kGetRechargeRecordURL paramsDic:paramDic fromDelegate:self    successBlock:^(AFHTTPRequestOperation *operation, id resultData)      {          [weakSelf requestRechargeRecordFinished:resultData];      }    failBlock:^(AFHTTPRequestOperation *operation, NSError *error) {        [weakSelf.HUD showToastWithText:kRequestFailString];    }];

写法2:
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

WS(ws)    ws.subListPickerView.subListSelectedBlock = ^(NSString *typeName, NSString *typeIdChildren) {        if (typeName == nil) {            [ws.choiceTypeBtn setTitle:ws.subListTypeNameArray[0] forState:UIControlStateNormal];            ws.typeIdChildren = ws.typeIdChildrenArray[0];        }else        {            [ws.choiceTypeBtn setTitle:typeName forState:UIControlStateNormal ];            ws.typeIdChildren = typeIdChildren;        }    };

总结经验教训:
1.回掉里面的属性一定要弱引用,否组内存不释放
2.判断是否内存释放,离开对应控制器操作时,是否走到-(void)delloc函数

- (void)dealloc{    [[NSNotificationCenter defaultCenter] removeObserver:self];}
原创粉丝点击