paulw11/Seam3

Should I create a second persistent store for this library in my app?

Closed this issue · 5 comments

bcye commented

Hello,

I have the following code:

class CoreDataStack {

    var smStore: SMStore?
    
    lazy var managedObjectContext: NSManagedObjectContext = {
        let container = self.persistentContainer
        return container.viewContext
    }()

    private lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "WHIR")
        container.loadPersistentStores { (_, error) in
            if let error = error as NSError? {
                // TODO: Find way to alert the user from here
                print(error)
            }
        }
        return container
    }()
    
    lazy var cloudPersistentContainer: NSPersistentContainer = {
        SMStore.registerStoreClass()
        let container = NSPersistentContainer(name: "WHIRCloud")
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        
        if let applicationDocumentsDirectory = urls.last {
            let url = applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
            let storeDescription = NSPersistentStoreDescription(url: url)
            storeDescription.type = SMStore.type
            container.persistentStoreDescriptions=[storeDescription]
            container.loadPersistentStores(completionHandler: { (storeDescription, error) in
                if let error = error as NSError? {
                    // Replace this implementation with code to handle the error appropriately.
                    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                     */
                    fatalError("Unresolved error \(error), \(error.userInfo)")
                }
            })
            return container
        }
        
        fatalError("Unable to access documents directory")
        
    }()
}

First, can I use to different stores or how would I go about this?
Second in the line let url = applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite"), do I need to replace SingleViewCoreData.sqlite with something else? If so, what do I need to replace it with?

Generally the idea of Seam3 is that CloudKit is hidden from your app as much as possible and you just use Core Data as normal. So, you wouldn't typically create a second persistent store, except in the case where you have an existing app that uses local-only core data and you want to migrate it to Seam3; Then you do need to create a second persistent store and migrate the existing data to it.

bcye commented

So I can't integrate it into the existing persistent store that I'm using with my app now? How could I migrate the data and would that be a one-time operation, because I've also read something about persistent store coordinator. Would that be a solution?

bcye commented

I mean basically, the code is run every app startup, so couldn't I just add the seam code to my persistent store?

Sorry, I haven't looked into core data in a while and don't know exactly how everything works.

#59 offers advice on migrating to a new persistent store; This would be a one-time operation. Essentially you need to migrate your existing sqlite store to a Seam store.

bcye commented

Thank you.