Created
July 3, 2017 15:48
-
-
Save eduardochiaro/348ed728745be4f2ac315ee3a6357df3 to your computer and use it in GitHub Desktop.
Xcode 8.3, Swift 3.1 - Using common SQLite database as persistent container.
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
lazy var persistentContainer: NSPersistentContainer = { | |
let sqlitePath = Bundle.main.url(forResource: "DATABASE_NAME", withExtension: "sqlite") | |
if FileManager.default.fileExists(atPath: sqlitePath!.path) { | |
let container = NSPersistentContainer(name: "DATABASE_NAME") | |
let description = NSPersistentStoreDescription(url: sqlitePath!) | |
description.shouldInferMappingModelAutomatically = true | |
description.shouldMigrateStoreAutomatically = true | |
container.persistentStoreDescriptions = [description] | |
container.loadPersistentStores(completionHandler: { (storeDescription, error) in | |
if let error = error as NSError? { | |
fatalError("Unresolved error \(error), \(error.userInfo)") | |
} | |
}) | |
return container | |
} | |
let container = NSPersistentContainer(name: "DATABASE_NAME") | |
container.loadPersistentStores(completionHandler: { (storeDescription, error) in | |
if let error = error as NSError? { | |
fatalError("Unresolved error \(error), \(error.userInfo)") | |
} | |
}) | |
return container | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment