I’m having trouble rebuilding my core data stack (using Swift 3). My data is essentially an offline cache, never a source of truth, and is read-only, so when I update my model I should be able to just tear down my core data stack and rebuild it but I don’t know how.
There are plenty of resources showing how to do this in Objective C, and plenty of resources showing how to migrate my model. There are plenty of throwaway comments like “just rebuild your stack”, and the RW book even says this.
Can anyone shed some light on how to properly do it?
I’m currently using a CoreDataStack class as described in Core Data by Tutorials v3.0.
public class CoreDataStack {
private let modelName: String
lazy var mainContext: NSManagedObjectContext = {
return self.storeContainer.viewContext
}()
private lazy var storeContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: self.modelName)
container.loadPersistentStores { (storeDescription, error) in
if let error = error as NSError? {
print("Unresolved error \(error), \(error.userInfo)")
// TODO: rebuild core data stack...
}
}
return container
}()
// ...
}
Thanks in advance
Tim