Created
June 17, 2022 10:47
-
-
Save jeneiv/276a62c6261775e079b3cfdbe7ce7f24 to your computer and use it in GitHub Desktop.
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
class PersistencyController { | |
enum SetupResult { | |
case success | |
case error(error: Error) | |
} | |
static let shared = PersistencyController() | |
private var persistentContainer: NSPersistentContainer! | |
var viewContext : NSManagedObjectContext { | |
return persistentContainer.viewContext | |
} | |
var backgroundContext : NSManagedObjectContext { | |
return persistentContainer.newBackgroundContext() | |
} | |
private init() {} | |
func setup(containerName: String, completion: ((SetupResult) -> Swift.Void)?) { | |
guard persistentContainer == nil else { | |
return | |
} | |
persistentContainer = NSPersistentContainer(name: containerName) | |
persistentContainer.loadPersistentStores { (storeDescription : NSPersistentStoreDescription, error : Error?) in | |
if let error = error { | |
completion?(.error(error: error)) | |
} | |
else { | |
self.persistentContainer.viewContext.automaticallyMergesChangesFromParent = true | |
completion?(.success) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment