Created
November 28, 2014 17:17
-
-
Save hajipy/8f0b1dbb4edbd029fc96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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