-
Apple 官方的建議是,傳進(jìn) Block 之前,把 ‘self’ 轉(zhuǎn)換成 weak automatic 的變量,這樣在 Block 中就不會(huì)出現(xiàn)對(duì) self 的強(qiáng)引用。如果在 Block 執(zhí)行完成之前,self 被釋放了,weakSelf 也會(huì)變?yōu)?nil。
__weak __typeof__(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[weakSelf doSomething];
});
clang 的文檔表示,在 doSomething 內(nèi),weakSelf 不會(huì)被釋放。但,下面的情況除外:__weak __typeof__(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__strong __typeof(self) strongSelf = weakSelf;
[strongSelf doSomething];
[strongSelf doOtherThing];
});
-
在 doSomething 中,weakSelf 不會(huì)變成 nil,不過(guò)在 doSomething 執(zhí)行完成,調(diào)用第二個(gè)方法 doOtherThing 的時(shí)候,weakSelf 有可能被釋放,于是,strongSelf 就派上用場(chǎng)了:
__weak __typeof__(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__strong __typeof(self) strongSelf = weakSelf;
[strongSelf doSomething];
[strongSelf doOtherThing];
});
-
例:雖然在這個(gè)方法中在block內(nèi)部調(diào)用了self,但是block不是self的屬性不會(huì)形成循環(huán)引用
- (void)loadNewData
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"a"] = @"list";
parameters[@"c"] = @"data";
parameters[@"type"] = @(self.type);
[manager GET:basicUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//儲(chǔ)存maxtime
self.maxtime = responseObject[@"info"][@"maxtime"];
// 字典數(shù)組 -> 模型數(shù)據(jù)
self.topics = [XCAllItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
[self.tableView reloadData];
//結(jié)束刷新
[self.tableView.mj_header endRefreshing];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"網(wǎng)絡(luò)錯(cuò)誤"];
}];
//結(jié)束刷新
[self.tableView.mj_header endRefreshing];
}
-
例:這個(gè)方法中block內(nèi)部調(diào)用了self.manager是self的屬性,所以直接用self會(huì)形成循環(huán)引用
-(void)loadNewComments
{
[self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"a"]= @"dataList";
params[@"c"] = @"comment";
params[@"data_id"] = self.topic.ID;
params[@"hot"] = @1;
__weak typeof(self) weakSelf = self;
[self.manager GET:basicUrl parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject isKindOfClass:[NSArray class]]) {
//沒有評(píng)論數(shù)據(jù)
[self.tableView.mj_header endRefreshing];
return ;
}
//最熱評(píng)論
weakSelf.hotComments = [XCComment mj_objectArrayWithKeyValuesArray:responseObject[@"hot"]];
//最新評(píng)論
weakSelf.hotComments = [XCComment mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
[weakSelf.tableView reloadData];
[weakSelf.tableView.mj_header endRefreshing];
//判斷評(píng)論數(shù)據(jù)是否已經(jīng)加載完全
if (self.latestComments.count >= [responseObject[@"total"] intValue]) {
weakSelf.tableView.mj_footer.hidden = YES;
}
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[weakSelf.tableView.mj_footer endRefreshing];
}];
}
-
__strong 確保在 Block 內(nèi),strongSelf 不會(huì)被釋放。
-
總結(jié)
-
在 Block 內(nèi)如果需要訪問(wèn) self 的方法、屬性、變量,建議使用 weakSelf,weakSelf用會(huì)隨著上一級(jí)的銷毀一起銷毀。
-
如果在 Block 內(nèi)需要多次 訪問(wèn) self,則需要使用 strongSelf,如果block里面有一些任務(wù)即使 該頁(yè)面退出了還需要執(zhí)行完畢的,也需要使用strongSelf
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。