Skip to content

Instantly share code, notes, and snippets.

@andresr-dev
Last active February 20, 2023 20:14
Show Gist options
  • Save andresr-dev/4127e7687047b740e2db10d5fbf7a67e to your computer and use it in GitHub Desktop.
Save andresr-dev/4127e7687047b740e2db10d5fbf7a67e to your computer and use it in GitHub Desktop.
This is how to setup core data with in memory option and complete file protection
import CoreData
class DataController {
let container: NSPersistentContainer
/// Core Data initialization
/// - Parameter inMemory: For testing and previewing purposes, we create a temporary,
/// in-memory database by writing to /dev/null so our data is destroyed after our data finishes running.
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "Main")
if inMemory {
container.persistentStoreDescriptions.first?.url = URL(filePath: "/dev/null")
}
// Setup complete file protection for container
container.persistentStoreDescriptions.first?.setOption(FileProtectionType.complete as NSObject, forKey: NSPersistentStoreFileProtectionKey)
container.loadPersistentStores { storeDescription, error in
if let error {
fatalError("Error loading CoreData store: \(error.localizedDescription)")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment