我将问题跟踪到以下代码块:
self.dataSource.doNotAllowUpdates = YES;
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
} completion:^(BOOL finished) {
self.dataSource.doNotAllowUpdates = NO;
}];
基本上,如果我打电话给performBatchUpdates,然后立即调用dismissViewControllerAnimated,UIViewController会泄漏,并且该UIViewController的dealloc方法永远不会被调用. UIViewController永远挂起.
有人可以解释这个行为吗?我假设performBatchUpdates在一段时间间隔内运行,比如说500 ms,所以我假设在所述间隔之后,它将调用这些方法,然后触发dealloc.
修复似乎是这样的:
self.dataSource.doNotAllowUpdates = YES;
__weak __typeof(self)weakSelf = self;
[self.collectionView performBatchUpdates:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
if (strongSelf) {
[strongSelf.collectionView reloadItemsAtIndexPaths:@[indexPath]];
}
} completion:^(BOOL finished) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
if (strongSelf) {
strongSelf.dataSource.doNotAllowUpdates = NO;
}
}];
请注意,BOOL成员变量doNotAllowUpdates是我添加的一个变量,它阻止在执行performBatchUpdates的调用时执行任何类型的dataSource / collectionView更新.
我在网上搜索了解我们是否在performBatchUpdates中使用weakSelf / strongSelf模式,但没有在这个问题上找到任何具体内容.
我很高兴我能够找到这个bug的底部,但是我会喜欢一个更智能的iOS开发者向我解释这个我看到的这个行为.
解决方法
保留周期是由自己对collectionView有很强的引用引起的,而collectionView现在对自己有很强的引用.
必须始终假设在执行异步块之前,自己可能已被释放.为了安全处理这两件事必须做到:
>始终使用弱参考自身(或ivar本身)
>在将它作为一个nunnull传递之前,始终确认weakSelf存在
ParaM
更新:
在performBatchUpdates上进行一些日志记录证实了很多:
- (void)logperformBatchUpdates {
[self.collectionView performBatchUpdates:^{
NSLog(@"starting reload");
[self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
NSLog(@"finishing reload");
} completion:^(BOOL finished) {
NSLog(@"completed");
}];
NSLog(@"exiting");
}
打印:
starting reload finishing reload exiting completed
这表明在离开当前作用域之后,完成块被触发,这意味着它将异步调度回主线程.
您提到在批量更新后立即关闭视图控制器.我认为这是你问题的根源:
经过一些测试,我能够重新创建内存泄漏的唯一方法是在卸载之前调度工作.这是一个漫长的镜头,但你的代码看起来像这样吗?
- (void)breakIt {
// dispatch causes the view controller to get dismissed before the enclosed block is executed
dispatch_async(dispatch_get_main_queue(),^{
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
} completion:^(BOOL finished) {
NSLog(@"completed: %@",self);
}];
});
[self.presentationController.presentingViewController dismissViewControllerAnimated:NO completion:nil];
}
上面的代码导致在视图控制器上不被调用的dealloc.
如果你采取现有的代码,只需派遣(或执行选择器:在:)之后,你可能会解决这个问题.