Search code examples
iosswiftiphonecore-datacore-data-migration

How to move old project to PersistentContainer


I want to use persistent container to my old application which is having some live customers.

Current application is having the sqlite file saved in documents directory and my persistent container should points to that data.

Can anybody suggest me how to do this?


Solution

  • Before loading PersistentStores, we have to set persistentStoreDescription which is pointing to your already existed database location.

        let container = NSPersistentContainer(name: "DBName")
        let url = self.applicationDocumentsDirectory.appendingPathComponent("\(CoreDataManager.DBName).sqlite")
        let storeDescription = NSPersistentStoreDescription(url: url)
        container.persistentStoreDescriptions = [storeDescription]