diff --git a/Graph.podspec b/Graph.podspec index 83ade56b..bc419f48 100755 --- a/Graph.podspec +++ b/Graph.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'Graph' - s.version = '1.0.3' + s.version = '1.0.4' s.license = 'BSD' s.summary = 'An elegant data-driven framework for CoreData in Swift.' s.homepage = 'http://cosmicmind.io' diff --git a/Sources/GraphWatch.swift b/Sources/GraphWatch.swift index 6c913850..5afefe89 100644 --- a/Sources/GraphWatch.swift +++ b/Sources/GraphWatch.swift @@ -167,6 +167,69 @@ public extension Graph { // :name: managedObjectContextDidSave // internal func managedObjectContextDidSave(notification: NSNotification) { + if NSThread.isMainThread() { + notifyWatchers(notification) + } else { + dispatch_sync(dispatch_get_main_queue()) { [unowned self] in + self.notifyWatchers(notification) + } + } + } + + // + // :name: prepareForObservation + // + internal func prepareForObservation() { + NSNotificationCenter.defaultCenter().removeObserver(self) + NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextDidSaveNotification, object: context) + NSNotificationCenter.defaultCenter().addObserver(self, selector: "managedObjectContextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: context) + } + + // + // :name: addPredicateToObserve + // + internal func addPredicateToObserve(entityDescription: NSEntityDescription, predicate: NSPredicate) { + let entityPredicate: NSPredicate = NSPredicate(format: "entity.name == %@", entityDescription.name! as NSString) + let predicates: Array = [entityPredicate, predicate] + let finalPredicate: NSPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates) + masterPredicate = nil == masterPredicate ? finalPredicate : NSCompoundPredicate(orPredicateWithSubpredicates: [masterPredicate!, finalPredicate]) + } + + // + // :name: isWatching + // + internal func isWatching(key: String, index: String) -> Bool { + if nil == watchers[key] { + watchers[key] = Array(arrayLiteral: index) + return false + } + if watchers[key]!.contains(index) { + return true + } + watchers[key]!.append(index) + return false + } + + // + // :name: addWatcher + // + internal func addWatcher(key: String, value: String, index: String, entityDescriptionName: String, managedObjectClassName: String) { + if !isWatching(value, index: index) { + let entityDescription: NSEntityDescription = NSEntityDescription() + entityDescription.name = entityDescriptionName + entityDescription.managedObjectClassName = managedObjectClassName + let predicate: NSPredicate = NSPredicate(format: "%K LIKE %@", key as NSString, value as NSString) + addPredicateToObserve(entityDescription, predicate: predicate) + prepareForObservation() + } + } + + /** + Notifies watchers of changes within the ManagedObjectContext. + - Parameter notification: An NSNotification passed from the context + save operation. + */ + private func notifyWatchers(notification: NSNotification) { let moc: NSManagedObjectContext = context! moc.mergeChangesFromContextDidSaveNotification(notification) @@ -277,52 +340,4 @@ public extension Graph { } } } - - // - // :name: prepareForObservation - // - internal func prepareForObservation() { - NSNotificationCenter.defaultCenter().removeObserver(self) - NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextDidSaveNotification, object: context) - NSNotificationCenter.defaultCenter().addObserver(self, selector: "managedObjectContextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: context) - } - - // - // :name: addPredicateToObserve - // - internal func addPredicateToObserve(entityDescription: NSEntityDescription, predicate: NSPredicate) { - let entityPredicate: NSPredicate = NSPredicate(format: "entity.name == %@", entityDescription.name! as NSString) - let predicates: Array = [entityPredicate, predicate] - let finalPredicate: NSPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates) - masterPredicate = nil == masterPredicate ? finalPredicate : NSCompoundPredicate(orPredicateWithSubpredicates: [masterPredicate!, finalPredicate]) - } - - // - // :name: isWatching - // - internal func isWatching(key: String, index: String) -> Bool { - if nil == watchers[key] { - watchers[key] = Array(arrayLiteral: index) - return false - } - if watchers[key]!.contains(index) { - return true - } - watchers[key]!.append(index) - return false - } - - // - // :name: addWatcher - // - internal func addWatcher(key: String, value: String, index: String, entityDescriptionName: String, managedObjectClassName: String) { - if !isWatching(value, index: index) { - let entityDescription: NSEntityDescription = NSEntityDescription() - entityDescription.name = entityDescriptionName - entityDescription.managedObjectClassName = managedObjectClassName - let predicate: NSPredicate = NSPredicate(format: "%K LIKE %@", key as NSString, value as NSString) - addPredicateToObserve(entityDescription, predicate: predicate) - prepareForObservation() - } - } } diff --git a/Sources/Info.plist b/Sources/Info.plist index b1926f76..81c54c50 100644 --- a/Sources/Info.plist +++ b/Sources/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.0.3 + 1.0.4 CFBundleSignature ???? CFBundleVersion