[英]iOS using self.tableview in block make retain Cycle So leaks
in my UITableViewController
's loading Process, i fetch UITableView
's Data From ServerSingleton Class With Block, async network method 在我的
UITableViewController
的加载过程中,我从ServerSingleton Class With Block获取UITableView
的数据,异步网络方法
i give block, which Process UITableView
reloadData when data fetch Done, to that method 我给了块,当数据提取完成时,将
UITableView
reloadData处理为该方法
__weak typeof(MyTableViewController) *weakSelf = self;
NSURLSessionTask *task = [[ServerSingleton getSharedServerModule] getPostsBestWithCategory:self.categoryNameString
numberOf:10
fromLastPostID:nil
andBlock:^(NSArray *posts_,
NSError *error) {
if (!error) {
weakSelf.posts=posts_;
[weakSelf.tableView reloadData]; /////////Only This row make LEAK!!!!
[weakSelf addFooterActivity];
[weakSelf checkAndPlayWithContentOffset];
weakSelf.isLoadingMoreData=false;
}];
[UIAlertView showAlertViewForTaskWithErrorOnCompletion:task delegate:nil];
[headRefreshControl setRefreshingWithStateOfTask:task];
}
and this below is ServerSingleTon's method and when network request done, it process block which i gave it before 以下是ServerSingleTon的方法,当网络请求完成时,它是我之前给出的处理块
- (NSURLSessionDataTask *)getPostsBestWithCategory:(NSString *)categoryName
numberOf:(NSInteger)number
fromLastPostID:(NSString *)LastPostID
andBlock:( void (^)(NSArray *posts,
NSError *error))block{
AFHTTPSessionManager *client=[self getSessionManagerForInsecureDNS];
NSString *urlStr=@"urlString";
return [client GET:urlStr
parameters:nil
success:^(NSURLSessionTask *__unused task,id json){
NSArray *adaptedResult =[self PostArrayAdapterWithObject:json];
if (block) {
block([NSArray arrayWithArray:adaptedResult],nil);
}
}
failure:^(NSURLSessionTask *__unused task,NSError *error){
if (block) {
block([NSArray array],error);
}
}];
my problem is that my custom UITableView
controller get in retain Cycle thus cause leak. 我的问题是我的自定义
UITableView
控制器进入保留周期因此导致泄漏。
I figure out my problem is using self in block, so i change it to weakSelf, But It leaks too, and it was because of calling [weakSelf.tableView reloadData];
我发现我的问题是在块中使用self,所以我将其更改为weakSelf,但它也泄漏了,这是因为调用
[weakSelf.tableView reloadData];
when i delete this, it doesn't leak , but when the line exist it make retain cycle and leak 当我删除它时,它不会泄漏,但是当该行存在时,它会使保留周期和泄漏
what make leaks only to access UITableView
? 是什么让泄漏只能访问
UITableView
? but others not???? 但其他人不是????
i have tried using weak type of UITableView
itself, and UITableView
is initiated from nib so i connect With __weak
property 我试过使用弱类型的
UITableView
本身, UITableView
是从nib启动所以我连接__weak
属性
只是解决了同样的问题,你可能想检查tableView:cellForRowAtIndexPath:
的代码tableView:cellForRowAtIndexPath:
, self
可能保留在UITableViewCell
代码的某处,因为唯一的区别是[tableView reloadData]
。
You should not call [UITableView reloadData]
from a non-main thread, and this may be the cause of the leak you are seeing: 您不应该从非主线程调用
[UITableView reloadData]
,这可能是您看到泄漏的原因:
if (!error) {
weakSelf.posts=posts_;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.tableView reloadData];
});
...
You may want to use dispatch_sync()
in the above code, depending on what those methods after it do, or you may want to add them into dispatched block. 您可能希望在上面的代码中使用
dispatch_sync()
,具体取决于它之后的那些方法,或者您可能希望将它们添加到调度块中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.