2

Problem: I'm getting memory leaks in the function controllerDidChangeContent when I profile my app. Instruments is telling me that the leak should be in the line self.collectionView?.performBatchUpdates({ [unowned self] in. But how can there be a memory cycle since I have already added unowned self but with no success. Do you have any hints how to fix this leak?

Idea: Could this be caused through the Array private var blockOperations = [BlockOperation]() declared in the class? But it's not possible to set an Array to weak.

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()
}

}

Hans Bondoka
  • 437
  • 1
  • 4
  • 14

0 Answers0