Last active
February 20, 2023 20:14
-
-
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
This file contains hidden or 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
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