search

Home  >  Q&A  >  body text

ios - 用self.collectionView performBatchUpdates: 进行批量修改时的内存泄漏问题

我的代码是这样的:

[self.collectionView performBatchUpdates:^{
    
    [self.collectionView deleteItemsAtIndexPaths:deletedIndexpathes];
    if (emptySections.count > 0 ) { // delete empty sections
        [self.sectionNames removeObjectsAtIndexes:emptySections];
        [self.collectionView deleteSections:emptySections];
    }
    
} completion:nil];

根据我的理解, self对要执行的block是强引用,而block里面又对self进行了操作, 所以我觉得这里存在memory Cycle , 应该用weakSelf替换block里面的self,但是我用leak进行测试的时候并没有显示内存泄漏, 搜了网上的类似代码,也没有用weakSelf替换, 这里不存在内存泄漏的问题吗?

PHPzPHPz2773 days ago892

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 17:31:49

    Is self a strong reference to the block to be executed? Not necessarily. In your example, block is just passed as a parameter to collectionView's performBatchUpdates method. CollectionView does not hold a reference to block (I don't have the source code, but I can guess that collectionView does not need to hold block. ), just executed as a local variable. Unless collectionView has a strong attribute, block is assigned to that strong attribute in performBatchUpdates, so that collectionView will hold a strong reference to block.

    reply
    0
  • Cancelreply