Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. AppsTitude revised this gist Mar 5, 2015. 1 changed file with 0 additions and 4 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,7 @@
    // I just implemented that with Swift. So I would like to share my implementation.
    // First initialise an array of NSBlockOperations:
    var blockOperations: [NSBlockOperation] = []
    In controller will change, re-init the array:

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
    blockOperations.removeAll(keepCapacity: false)
    }

    // In the did change object method:
    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
  2. AppsTitude revised this gist Mar 5, 2015. 1 changed file with 1 addition and 5 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,13 @@
    // I just implemented that with Swift. So I would like to share my implementation.
    // First initialise an array of NSBlockOperations:

    var blockOperations: [NSBlockOperation] = []
    In controller will change, re-init the array:

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
    blockOperations.removeAll(keepCapacity: false)
    }

    // In the did change object method:

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

    if type == NSFetchedResultsChangeType.Insert {
    @@ -57,7 +56,6 @@ In controller will change, re-init the array:
    }

    // In the did change section method:

    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {

    if type == NSFetchedResultsChangeType.Insert {
    @@ -95,7 +93,6 @@ In controller will change, re-init the array:
    }

    // And finally, in the did controller did change content method:

    func controllerDidChangeContent(controller: NSFetchedResultsController) {
    collectionView!.performBatchUpdates({ () -> Void in
    for operation: NSBlockOperation in self.blockOperations {
    @@ -107,7 +104,6 @@ In controller will change, re-init the array:
    }

    // I personally added some code in the deinit method as well, in order to cancel the operations when the ViewController is about to get deallocated:

    deinit {
    // Cancel all block operations when VC deallocates
    for operation: NSBlockOperation in blockOperations {
  3. AppsTitude renamed this gist Mar 5, 2015. 1 changed file with 6 additions and 6 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    I just implemented that with Swift. So I would like to share my implementation.
    First initialise an array of NSBlockOperations:
    // I just implemented that with Swift. So I would like to share my implementation.
    // First initialise an array of NSBlockOperations:

    var blockOperations: [NSBlockOperation] = []
    In controller will change, re-init the array:

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
    blockOperations.removeAll(keepCapacity: false)
    }
    In the did change object method:
    // In the did change object method:

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

    @@ -56,7 +56,7 @@ In the did change object method:
    }
    }

    In the did change section method:
    // In the did change section method:

    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {

    @@ -94,7 +94,7 @@ In the did change section method:
    }
    }

    And finally, in the did controller did change content method:
    // And finally, in the did controller did change content method:

    func controllerDidChangeContent(controller: NSFetchedResultsController) {
    collectionView!.performBatchUpdates({ () -> Void in
    @@ -106,7 +106,7 @@ And finally, in the did controller did change content method:
    })
    }

    I personally added some code in the deinit method as well, in order to cancel the operations when the ViewController is about to get deallocated:
    // I personally added some code in the deinit method as well, in order to cancel the operations when the ViewController is about to get deallocated:

    deinit {
    // Cancel all block operations when VC deallocates
  4. AppsTitude revised this gist Mar 5, 2015. 1 changed file with 8 additions and 11 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,14 @@
    I just implemented that with Swift. So I would like to share my implementation.
    First initialise an array of NSBlockOperations:

    ```swift
    var blockOperations: [NSBlockOperation] = []
    In controller will change, re-init the array:

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
    blockOperations.removeAll(keepCapacity: false)
    }
    ```
    In the did change object method:
    ```swift

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

    if type == NSFetchedResultsChangeType.Insert {
    @@ -57,9 +55,9 @@ In the did change object method:
    )
    }
    }
    ```

    In the did change section method:
    ```swift

    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {

    if type == NSFetchedResultsChangeType.Insert {
    @@ -95,9 +93,9 @@ In the did change section method:
    )
    }
    }
    ```

    And finally, in the did controller did change content method:
    ```swift

    func controllerDidChangeContent(controller: NSFetchedResultsController) {
    collectionView!.performBatchUpdates({ () -> Void in
    for operation: NSBlockOperation in self.blockOperations {
    @@ -107,15 +105,14 @@ And finally, in the did controller did change content method:
    self.blockOperations.removeAll(keepCapacity: false)
    })
    }
    ```

    I personally added some code in the deinit method as well, in order to cancel the operations when the ViewController is about to get deallocated:
    ```swift

    deinit {
    // Cancel all block operations when VC deallocates
    for operation: NSBlockOperation in blockOperations {
    operation.cancel()
    }

    blockOperations.removeAll(keepCapacity: false)
    }
    ```
    }
  5. AppsTitude revised this gist Mar 5, 2015. 1 changed file with 10 additions and 7 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,16 @@
    I just implemented that with Swift. So I would like to share my implementation.
    First initialise an array of NSBlockOperations:

    ```swift
    var blockOperations: [NSBlockOperation] = []
    In controller will change, re-init the array:

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
    blockOperations.removeAll(keepCapacity: false)
    }
    ```
    In the did change object method:

    ```swift
    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

    if type == NSFetchedResultsChangeType.Insert {
    @@ -55,9 +57,9 @@ In the did change object method:
    )
    }
    }

    ```
    In the did change section method:

    ```swift
    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {

    if type == NSFetchedResultsChangeType.Insert {
    @@ -93,9 +95,9 @@ In the did change section method:
    )
    }
    }

    ```
    And finally, in the did controller did change content method:

    ```swift
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
    collectionView!.performBatchUpdates({ () -> Void in
    for operation: NSBlockOperation in self.blockOperations {
    @@ -105,9 +107,9 @@ And finally, in the did controller did change content method:
    self.blockOperations.removeAll(keepCapacity: false)
    })
    }

    ```
    I personally added some code in the deinit method as well, in order to cancel the operations when the ViewController is about to get deallocated:

    ```swift
    deinit {
    // Cancel all block operations when VC deallocates
    for operation: NSBlockOperation in blockOperations {
    @@ -116,3 +118,4 @@ I personally added some code in the deinit method as well, in order to cancel th

    blockOperations.removeAll(keepCapacity: false)
    }
    ```
  6. AppsTitude created this gist Mar 5, 2015.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,118 @@
    I just implemented that with Swift. So I would like to share my implementation.
    First initialise an array of NSBlockOperations:

    var blockOperations: [NSBlockOperation] = []
    In controller will change, re-init the array:

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
    blockOperations.removeAll(keepCapacity: false)
    }
    In the did change object method:

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

    if type == NSFetchedResultsChangeType.Insert {
    println("Insert Object: \(newIndexPath)")

    blockOperations.append(
    NSBlockOperation(block: { [weak self] in
    if let this = self {
    this.collectionView!.insertItemsAtIndexPaths([newIndexPath!])
    }
    })
    )
    }
    else if type == NSFetchedResultsChangeType.Update {
    println("Update Object: \(indexPath)")
    blockOperations.append(
    NSBlockOperation(block: { [weak self] in
    if let this = self {
    this.collectionView!.reloadItemsAtIndexPaths([indexPath!])
    }
    })
    )
    }
    else if type == NSFetchedResultsChangeType.Move {
    println("Move Object: \(indexPath)")

    blockOperations.append(
    NSBlockOperation(block: { [weak self] in
    if let this = self {
    this.collectionView!.moveItemAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
    }
    })
    )
    }
    else if type == NSFetchedResultsChangeType.Delete {
    println("Delete Object: \(indexPath)")

    blockOperations.append(
    NSBlockOperation(block: { [weak self] in
    if let this = self {
    this.collectionView!.deleteItemsAtIndexPaths([indexPath!])
    }
    })
    )
    }
    }

    In the did change section method:

    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {

    if type == NSFetchedResultsChangeType.Insert {
    println("Insert Section: \(sectionIndex)")

    blockOperations.append(
    NSBlockOperation(block: { [weak self] in
    if let this = self {
    this.collectionView!.insertSections(NSIndexSet(index: sectionIndex))
    }
    })
    )
    }
    else if type == NSFetchedResultsChangeType.Update {
    println("Update Section: \(sectionIndex)")
    blockOperations.append(
    NSBlockOperation(block: { [weak self] in
    if let this = self {
    this.collectionView!.reloadSections(NSIndexSet(index: sectionIndex))
    }
    })
    )
    }
    else if type == NSFetchedResultsChangeType.Delete {
    println("Delete Section: \(sectionIndex)")

    blockOperations.append(
    NSBlockOperation(block: { [weak self] in
    if let this = self {
    this.collectionView!.deleteSections(NSIndexSet(index: sectionIndex))
    }
    })
    )
    }
    }

    And finally, in the did controller did change content method:

    func controllerDidChangeContent(controller: NSFetchedResultsController) {
    collectionView!.performBatchUpdates({ () -> Void in
    for operation: NSBlockOperation in self.blockOperations {
    operation.start()
    }
    }, completion: { (finished) -> Void in
    self.blockOperations.removeAll(keepCapacity: false)
    })
    }

    I personally added some code in the deinit method as well, in order to cancel the operations when the ViewController is about to get deallocated:

    deinit {
    // Cancel all block operations when VC deallocates
    for operation: NSBlockOperation in blockOperations {
    operation.cancel()
    }

    blockOperations.removeAll(keepCapacity: false)
    }