Created
September 28, 2014 07:10
-
-
Save Harinder/bc379bc95fec2e043043 to your computer and use it in GitHub Desktop.
Save context and propagate changes to any parent contexts
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 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