How to rebuild a core data stack?

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

@timdavey. Thanks very much for your question, and my apologies for the delay in responding to you. Can you please elaborate what you mean by wanting to “tear down” your Core Data stack? In theory, once you have your stack, you really shouldn’t have to deal with it again. The ManagedObjectContext is the object that you would be actively using within your app.

If you can provide more details, I’d be happy to investigate further.

All the best!