0

I use this code to initialize Core Data:

import Cocoa import CoreData class DataController: NSObject { var persistentContainer: NSPersistentContainer! var context: NSManagedObjectContext! override init() { persistentContainer = NSPersistentContainer(name: "Highlightings") persistentContainer.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) context = persistentContainer.viewContext } } 

How can I specify a .sqlite file where Core Data should save data?

1 Answer 1

2

You could go back to the older approach without NSPersistentContainer. The old API has not been deprecated.

If you're using NSPersistentContainer, you can change the store location using NSPersistentStoreDescription. Something like

let container = NSPersistentContainer(name: "ContinerName") let storeURL = // Initialize to whatever URL you want let description = NSPersistentStoreDescription(url: storeURL) container.persistentStoreDescriptions = [ description ] container.loadPersistentStores(completionHandler: { (storeDescription, error) in // ... } 
Sign up to request clarification or add additional context in comments.

1 Comment

With the new approach the .sqlite file is located in ~/Library/Application Support/YOURAPPNAME/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.