Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Harinder/bc379bc95fec2e043043 to your computer and use it in GitHub Desktop.
Save Harinder/bc379bc95fec2e043043 to your computer and use it in GitHub Desktop.
Save context and propagate changes to any parent contexts
import CoreData
extension NSManagedObjectContext {
/// Commit unsaved changes and propagate them to ancestor contexts, if any.
///
/// :param: error A pointer to an NSError object returned on failure
/// :returns: true if the operation succeeds, false otherwise
@objc(kdj_saveToPersistentStore:)
public func saveToPersistentStore(error: NSErrorPointer) -> Bool {
if !self.save(error) {
return false
}
// If this is a child context, recursively invoke this method
// on its parent context
if let parentContext = self.parentContext {
var parentResult: Bool = false
parentContext.performBlockAndWait {
parentResult = parentContext.saveToPersistentStore(error)
}
return parentResult
}
else {
return true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment