1
var blockOperations = [NSBlockOperation]()
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

    if type == .Insert {
        blockOperations.append(NSBlockOperation(block: {
            self.collectionView?.insertItemsAtIndexPaths([newIndexPath!])

        }))
    }
}
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
    collectionView?.performBatchUpdates({
        for operation in self.blockOperations {
            operation.start()

        }
        }, completion: { (completed) in
           print("completed")
    })

}

This is my code for inserting messages into my collection view.

I need block operations so that when a message is delayed it can be inserted in a block matter.

The problem is every time I dismiss the viewController the print("completed") duplicates. which means that i have a memory leak.

And deinit never gets called unless I remove block operations, how can I release the block operation when I leave the viewController?

Dejan Skledar
  • 11,280
  • 7
  • 44
  • 70
slimboy
  • 1,633
  • 2
  • 22
  • 45
  • 1
    Refer [this so post](http://stackoverflow.com/questions/36730768/swift-nsblockoperation-leak-cannot-make-nsblockoperation-weak) , it may help! – Ketan Parmar Sep 06 '16 at 07:50
  • 1
    see [Resolving Strong Reference Cycles for Closures](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID57). – bluedome Sep 06 '16 at 07:53

0 Answers0