Skip to content

Instantly share code, notes, and snippets.

@eduardochiaro
Created July 3, 2017 15:48
Show Gist options
  • Save eduardochiaro/348ed728745be4f2ac315ee3a6357df3 to your computer and use it in GitHub Desktop.
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.
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