如何修复NSFetchedResultsController controllerDidChangeContent中的内存泄漏

时间:2019-05-04 13:59:16

标签: swift closures automatic-ref-counting nsblockoperation

问题: 剖析我的应用程序时,我在功能controllerDidChangeContent中遇到内存泄漏。仪器告诉我,泄漏应该在self.collectionView?.performBatchUpdates({ [unowned self] in行中。但是,由于我已经添加了无主的自我,但没有成功,怎么会有一个记忆周期。您对如何解决此泄漏有任何提示吗?

想法: 这可能是由于在类中声明的数组private var blockOperations = [BlockOperation]()引起的吗?但是不可能将数组设置为弱。

extension CollectionViewController: NSFetchedResultsControllerDelegate {

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    self.blockOperations = []
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any,
                at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
    var operation: BlockOperation
    switch type {
    case .insert:
        guard let newIndexPath = newIndexPath else { return }
        operation = BlockOperation { [unowned self] in
            self.collectionView?.insertItems(at: [newIndexPath])
        }
    case .delete:
        guard let indexPath = indexPath else { return }
        operation = BlockOperation { [unowned self] in
            self.collectionView?.deleteItems(at: [indexPath])
        }
    case .update:
        guard let newIndexPath = newIndexPath else { return }
        operation = BlockOperation { [unowned self] in
            self.collectionView?.reloadItems(at: [newIndexPath])
        }
    case .move:
        guard let indexPath = indexPath else { return }
        guard let newIndexPath = newIndexPath else { return }
        operation = BlockOperation { [unowned self] in
            self.collectionView?.deleteItems(at: [indexPath])
            self.collectionView?.insertItems(at: [newIndexPath])
        }
    }
    blockOperations.append(operation)
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    // in the next line there is the leak shown in Instruments
    self.collectionView?.performBatchUpdates({ [unowned self] in
        for block in self.blockOperations {
            block.start() }
    }, completion: { [unowned self] _ in
        self.blockOperations.removeAll()
    })
    updateView()
}

}

0 个答案:

没有答案