This is an example code that generate error ...
Why I cannot loadPersistentStores with 2 sqlite files ?
import CoreData
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentContainer
let container2: NSPersistentContainer
func urlApplicationSupportDirectory() -> URL {
let paths = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
func sqliteURL() -> URL {
let url = urlApplicationSupportDirectory().appendingPathComponent("MM1.SQLite")
return url
}
func sqliteURL2() -> URL {
let url = urlApplicationSupportDirectory().appendingPathComponent("MM2.SQLite")
return url
}
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "testCoreData")
container2 = NSPersistentContainer(name: "testCoreData")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
let url = sqliteURL()
print(url.absoluteString)
let description = NSPersistentStoreDescription(url: url)
description.shouldMigrateStoreAutomatically = true
description.shouldInferMappingModelAutomatically = true
container.persistentStoreDescriptions = [description]
container.viewContext.automaticallyMergesChangesFromParent = true
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
let url2 = sqliteURL2()
print(url2.absoluteString)
let description2 = NSPersistentStoreDescription(url: url2)
description2.shouldMigrateStoreAutomatically = true
description2.shouldInferMappingModelAutomatically = true
container2.persistentStoreDescriptions = [description2]
container2.viewContext.automaticallyMergesChangesFromParent = true
container2.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
}
}