     
 }
 
-// MARK: -
-
-fileprivate final class HelperPersistentContainer<Context: NSManagedObjectContext>: NSPersistentContainer {
-    
-    fileprivate override lazy var viewContext: NSManagedObjectContext = {
-        let context = Context(concurrencyType: .mainQueueConcurrencyType)
-
-        context.persistentStoreCoordinator = self.persistentStoreCoordinator
-        
-        context.automaticallyMergesChangesFromParent = true
-        context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
-        
-        
-
-        return context
-    }()
-    
-    fileprivate override func newBackgroundContext() -> NSManagedObjectContext {
-        let context = Context(concurrencyType: .privateQueueConcurrencyType)
-        
-        context.persistentStoreCoordinator = self.persistentStoreCoordinator
-        
-        context.automaticallyMergesChangesFromParent = true
-        context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
-        
-        return context
-    }
-    
-}
-
-
-// MARK: -
-
-extension PersistentContainer {
-
-    public enum StorageType {
-        case disk
-        case memory
-    }
-
-}
-
-// MARK: -
-
-extension PersistentContainer {
-
-    public enum Error: Swift.Error {
-        case invalidName
-        case invalidManagedObjectModelURL
-        case managedObjectModelNotFound
-        case invalidPersistentStoreURL
-    }
-
-}
+
+// MARK: -
+
+public enum StorageType {
+    case disk
+    case memory
+}
+
+// MARK: -
+
+public enum PersistentContainerError: Swift.Error {
+    case invalidName
+    case invalidManagedObjectModelURL
+    case managedObjectModelNotFound
+    case invalidPersistentStoreURL
+}
+
 
 // MARK: -
 


// MARK: -

open class PersistentContainer<Context: NSManagedObjectContext> {
    
    // MARK: -
    
    fileprivate let rawValue: NSPersistentContainer
    fileprivate let enableUbiquity: Bool

    // MARK: -
    
    public init(bundle: Bundle = Bundle.main, storageType: StorageType = .disk, name: String? = nil, managedObjectModel: NSManagedObjectModel? = nil, managedObjectModelURL: URL? = nil, persistentStoreURL: URL? = nil, ubiquitousContainerIdentifier: String? = nil, ubiquitousContentRelativePath: String? = nil, ubiquitousContentName: String? = nil) throws {
        //
        guard let name = name ?? bundle.bundleIdentifier else {
            throw Error.invalidName
        }
        
        guard let managedObjectModelURL = managedObjectModelURL ?? bundle.managedObjectModelURL(forManagedObjectModelName: name) else {
            throw Error.invalidManagedObjectModelURL
        }
        
        guard let managedObjectModel = managedObjectModel ?? NSManagedObjectModel(contentsOf: managedObjectModelURL) ?? NSManagedObjectModel.mergedModel(from: [bundle]) else {
            throw Error.managedObjectModelNotFound
        }

        guard let persistentStoreURL = persistentStoreURL ?? bundle.persistentStoreURL(forManagedObjectModelName: name, applicationName: name) else {
            throw Error.invalidPersistentStoreURL
        }
        
        //
        self.rawValue = HelperPersistentContainer(name: name, managedObjectModel: managedObjectModel)
        self.enableUbiquity = (ubiquitousContainerIdentifier != nil)
        
        //
        if storageType == .disk {

        }
        
        //
        self.persistentStoreDescriptions = [persistentStoreDescription]
        
        //
        var outError: Swift.Error?
        
        self.loadPersistentStores { description, error in
            if let error = error {
                outError = error
                return
            }
            
            if self.enableUbiquity {
                NotificationCenter.default.addObserver(self, selector: #selector(type(of: self).persistentStoreDidImportUbiquitousContentChanges(notification:)), name: .NSPersistentStoreDidImportUbiquitousContentChanges, object: self.persistentStoreCoordinator)
            }
        }
        
        if let outError = outError {
            throw outError
        }
        
        //
        self.viewContext.automaticallyMergesChangesFromParent = true
        self.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    }
    
    deinit {
        if self.enableUbiquity {
            NotificationCenter.default.removeObserver(self, name: .NSPersistentStoreDidImportUbiquitousContentChanges, object: self.persistentStoreCoordinator)
        }
    }
    
    // MARK: -

    open override func newBackgroundContext() -> NSManagedObjectContext {
        let context = super.newBackgroundContext()

        context.automaticallyMergesChangesFromParent = true
        context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

        return context
    }
    
    // MARK: -
    
    @objc private func persistentStoreDidImportUbiquitousContentChanges(notification: Notification) {
        let context = self.viewContext.parent ?? self.viewContext
        
        context.perform {
            context.mergeChanges(fromContextDidSave: notification)
