Skip to content

Instantly share code, notes, and snippets.

@hajipy
Created November 28, 2014 17:17
Show Gist options
  • Save hajipy/8f0b1dbb4edbd029fc96 to your computer and use it in GitHub Desktop.
Save hajipy/8f0b1dbb4edbd029fc96 to your computer and use it in GitHub Desktop.
import Cocoa
class ArrayControllerBugFixed: NSArrayController {
override func awakeFromNib() {
super.awakeFromNib()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("coreDataEntityDidChange:"), name: NSManagedObjectContextObjectsDidChangeNotification, object: self.managedObjectContext)
}
func coreDataEntityDidChange(notification: NSNotification) {
if self.sortDescriptors?.count > 0 {
if let userInfo = notification.userInfo {
let insertedEntityNames = userInfo[NSInsertedObjectsKey]?.valueForKeyPath("entity.name") as? NSSet ?? NSSet()
let updatedEntityNames = userInfo[NSUpdatedObjectsKey]?.valueForKeyPath("entity.name") as? NSSet ?? NSSet()
let deletedEntityNames = userInfo[NSDeletedObjectsKey]?.valueForKeyPath("entity.name") as? NSSet ?? NSSet()
if let entityName = self.entityName {
if insertedEntityNames.containsObject(entityName) || updatedEntityNames.containsObject(entityName) || deletedEntityNames.containsObject(entityName) {
self.rearrangeObjects()
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment