         onTypeChange = nil
         removeObserver()
     }
-    
+
     private func observe() {
         NSNotificationCenter.defaultCenter().addObserver(self, selector: "didChangeManagedObjectContext:", name: NSManagedObjectContextObjectsDidChangeNotification, object: CoreDataStack.sharedInstance.currentContext())
     }
-    
+
     public func removeObserver() {
         NSNotificationCenter.defaultCenter().removeObserver(self)
     }

    private var observedSetCopy: Set<NSManagedObject>?
    private var observedType: NSManagedObject.Type?
    private lazy var observedTypeFilter: (NSManagedObject) -> Bool = {(object) -> Bool in return object.isKindOfClass(self.observedType!)}
    
    private var onObjectsChange: ((updatedObjects: Set<NSManagedObject>, deletedObjects: Set<NSManagedObject>) -> ())?
    private var onSetChange: ((insertedObjects: Set<NSManagedObject>, removedObjects: Set<NSManagedObject>) -> ())?
    private var onTypeChange: ((insertedObjects: Set<NSManagedObject>, deletedObjects: Set<NSManagedObject>) -> ())?

    public init() {
    }
    
    deinit {
        removeObserver()
    }
    
    public func observe(managedObjects managedObjects: [NSManagedObject], onChange: (changedObjects: Set<NSManagedObject>, deletedObjects: Set<NSManagedObject>) -> ()) {
        reset()
        observedObjects = Set(managedObjects)
        self.onObjectsChange = onChange
        observe()
    }
    
    public func observe(managedObject managedObject: NSManagedObject, onChange: (changedObjects: Set<NSManagedObject>, deletedObjects: Set<NSManagedObject>) -> ()) {
        observe(managedObjects: [managedObject], onChange: onChange)
    }
    
    public func observe(set set: NSSet, onChange: (insertedObjects: Set<NSManagedObject>, removedObjects: Set<NSManagedObject>) -> ()) {
        reset()
        observedNSSet = set
        self.onSetChange = onChange
        observe()
    }
    
    public func observe(type type: NSManagedObject.Type, onChange: ((insertedObjects: Set<NSManagedObject>, deletedObjects: Set<NSManagedObject>) -> ())? = nil) {
        reset()
        observedType = type
        self.onTypeChange = onChange
        observe()
    }
    
    private func reset() {
        observedObjects = nil
        observedNSSet = nil

        let updatedObjects = {(notification.userInfo?[NSUpdatedObjectsKey] as? NSSet)?.allObjects as? [NSManagedObject] ?? []}
        let deletedObjects = {(notification.userInfo?[NSDeletedObjectsKey] as? NSSet)?.allObjects as? [NSManagedObject] ?? []}
        let insertedObjects = {(notification.userInfo?[NSInsertedObjectsKey] as? NSSet)?.allObjects as? [NSManagedObject] ?? []}

        // Detect if observed objects got changed or deleted
        if let observedObjects = self.observedObjects, onChange = self.onObjectsChange {
            let updatedObjects = observedObjects.intersect(updatedObjects())
