Created
June 29, 2018 12:26
-
-
Save jonathanduty/64a27382ffe8c441c59da290ffb0206b to your computer and use it in GitHub Desktop.
NSManagedObjectContext+Extensions.swift
This file contains hidden or 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 | |
public extension NSManagedObjectContext { | |
@discardableResult func saveToStore() -> NSError? { | |
var currentContext: NSManagedObjectContext? = self | |
var error: NSError? = nil | |
var hasChanges = true | |
while let context = currentContext, (error == nil && hasChanges) { | |
context.performAndWait { | |
hasChanges = context.hasChanges | |
if hasChanges { | |
do { | |
try context.save() | |
} | |
catch(let saveError) { | |
error = saveError as NSError | |
} | |
} | |
} | |
currentContext = context.parent | |
} | |
return error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment