0

I had been using core data for a long time in my app, with the standard setup, but when I changed the NSPersistentContainer, everything was deleted. I expected that the new NSPersistentContainer would be empty, but then when I went back to the original NSPersistentContainer there was now nothing in there either (beforehand over 10,000 objects)

Original standard setup

lazy var persistentContainer: NSPersistentContainer = {

    let container = NSPersistentContainer(name: "MyApp")

    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {

            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

But when I changed the Persistent container to share the app data with an extension using the following code, the app data all seemingly disappeared.

lazy var persistentContainer: NSPersistentContainer = {

    let container = NSCustomPersistentContainer(name: "MyApp")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {

            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

class NSCustomPersistentContainer: NSPersistentContainer {

    override open class func defaultDirectoryURL() -> URL {
        var storeURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.MyApp")
        storeURL = storeURL?.appendingPathComponent("MyApp.sqlite")
        return storeURL!
    }

}

When I went back to using the previous setup - i.e.

let container = NSPersistentContainer(name: "MyApp")

instead of

let container = NSCustomPersistentContainer(name: "MyApp")

there is no data returned from the database. Is it possible that changing the location of the persistent container could really delete all the data in the original container. I presumed it would create two separate containers, and so I could just switch back to the original one and everything would still be there.

Maciej Gad
  • 1,701
  • 16
  • 21
TimWhiting
  • 2,405
  • 5
  • 21
  • 41
  • As the name `defaultDirectoryURL` implies the URL must point to a directory not to the `sqlite` file. – vadian Dec 18 '19 at 11:30
  • @vadian The `defaultDirectoryURL` is used to set the location of the shared directory for the app and the extension. But why would changing the location of the `NSPersistentContainer` to this location delete all the original information in the original container? – TimWhiting Dec 18 '19 at 11:40
  • Maybe you have to set the store URL of the container explicitly back to the default value. – vadian Dec 18 '19 at 11:42
  • @vadian When I use `let container = NSPersistentContainer(name: "MyApp")` instead of `let container = NSCustomPersistentContainer(name: "MyApp")` i presume it automatically does this, because it is no longer overriding the `defaultDirectoryURL()` property. Or is that not the case? – TimWhiting Dec 18 '19 at 11:49
  • Yes, but check the `url` of the persistent store (not the container) and check if the database files are present at that URL – vadian Dec 18 '19 at 11:53
  • @vadian The `storeDescription.url` gives the expected location `file:///var/mobile/Containers/Data/Application/.../Library/Application%20Support/MyApp.sqlite` but the original data is not still inside the .sqlite file. So it has correctly set back to the original location but all the original data has disappeared in the process of changing the `defaultDirectoryUrl` – TimWhiting Dec 18 '19 at 11:55

0 Answers0