Seeding user's core data store with a pre-made core data database

So I have this technique I've found I thought would work to seed a core data db with one I've made. My goal list to take a sqlite database (made with a rough companion app in swiftui), filled with content, that will be part of the application bundle. The goal is to copy that data into the user's core data store and allow them to favorite, make certain changes to the items loaded into the database.

Is this technique I'm trying to use best for read-only databases or could I be implementing this wrong? Or is my best bet to copy over the sqlite once with FileManager?

This is. my persistent container code:

Code Block    
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "UserStore")
if isFirstLaunch() {
let seededDataURL = Bundle.main.url(forResource: "PopulatedStore", withExtension: "sqlite")
if let seededDataURL = seededDataURL {
let persistentDescription = NSPersistentStoreDescription(url:seededDataURL)
persistentDescription.shouldInferMappingModelAutomatically = true
persistentDescription.shouldMigrateStoreAutomatically = true
container.persistentStoreDescriptions = [persistentDescription]
}
}
container.loadPersistentStores { (storeDescription, error) in
if let error = error {
print("error loading persistent store! \(error)")
}
}
return container
}()


Thank you

Accepted Reply

So I followed these instructions https://stackoverflow.com/questions/40093585/swift-3-preload-from-sql-files-in-appdelegate/40107699 after finding this thread https://developer.apple.com/forums/thread/61745 and found it working well - the issue was that I needed to copy that seed database into the documents folder, then reference it there via NSPersistentStoreDescription, for anyone with the same circumstances.

Code Block          
  if FileManager.default.fileExists(atPath:seedDataDestination.path) == false {
                do {
                    try FileManager.default.copyItem(at: seededDataURL, to: seedDataDestination)
                } catch {
                    print("Error copying seeded data to documents directory!")
                }  
        }
            let persistentDescription = NSPersistentStoreDescription(url:seedDataDestination)


Replies

Wanted to follow up to say that the current functionality works when first launch is true, but otherwise loads the empty store. If I remove the first launch flag, any saves done don't persist.
So I followed these instructions https://stackoverflow.com/questions/40093585/swift-3-preload-from-sql-files-in-appdelegate/40107699 after finding this thread https://developer.apple.com/forums/thread/61745 and found it working well - the issue was that I needed to copy that seed database into the documents folder, then reference it there via NSPersistentStoreDescription, for anyone with the same circumstances.

Code Block          
  if FileManager.default.fileExists(atPath:seedDataDestination.path) == false {
                do {
                    try FileManager.default.copyItem(at: seededDataURL, to: seedDataDestination)
                } catch {
                    print("Error copying seeded data to documents directory!")
                }  
        }
            let persistentDescription = NSPersistentStoreDescription(url:seedDataDestination)