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

Swift 如何修复NSFetchedResultsController controllerDidChangeContent中的内存泄漏,swift,closures,automatic-ref-counting,nsblockoperation,Swift,Closures,Automatic Ref Counting,Nsblockoperation,问题: 当我评测我的应用程序时,函数控制器IDChangeContent中出现内存泄漏。Instruments告诉我泄漏应该在self.collectionView?行中。performbatchUpdate(中的{[unowned self])。但是既然我已经添加了unowned self,但没有成功,怎么会有内存周期呢。你有什么提示如何修复此泄漏 创意: 这可能是由类中声明的数组private var blockOperations=[BlockOperation]()引起的吗?但不可能将数

问题: 当我评测我的应用程序时,函数控制器IDChangeContent中出现内存泄漏。Instruments告诉我泄漏应该在
self.collectionView?行中。performbatchUpdate(
中的{[unowned self])。但是既然我已经添加了unowned self,但没有成功,怎么会有内存周期呢。你有什么提示如何修复此泄漏

创意: 这可能是由类中声明的数组
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()
}
扩展集合视图控制器:NSFetchedResultsControllerDelegate{
func controllerWillChangeContent(\ucontroller:NSFetchedResultsController){
self.blockOperations=[]
}
func控制器(u控制器:NSFetchedResultsController,didChange对象:Any,
在indexPath:indexPath?处,对于类型:NSFetchedResultsChangeType,newIndexPath:indexPath?){
var操作:BlockOperation
开关类型{
案例.插入:
guard let newindepath=newindepath else{return}
操作=块操作{[unowned self]在
self.collectionView?.insertItems(位于:[newIndexPath])
}
案例.删除:
guard let indepath=indepath else{return}
操作=块操作{[unowned self]在
self.collectionView?.deleteItems(位于:[indexPath])
}
案例。更新:
guard let newindepath=newindepath else{return}
操作=块操作{[unowned self]在
self.collectionView?.reloadItems(位于:[newIndexPath])
}
案件.动议:
guard let indepath=indepath else{return}
guard let newindepath=newindepath else{return}
操作=块操作{[unowned self]在
self.collectionView?.deleteItems(位于:[indexPath])
self.collectionView?.insertItems(位于:[newIndexPath])
}
}
blockOperations.append(操作)
}
func controllerDidChangeContent(\控制器:NSFetchedResultsController){
//下一行是仪表中显示的泄漏
self.collectionView?.performbatchUpdate({[unowned self]在中)
用于自阻塞操作中的阻塞{
block.start()}
},完成:{[unowned self]\uuIn
self.blockOperations.removeAll()
})
updateView()
}
}