swiftdata

SwiftData: Migration Error: How to remove all data from Xcode?


I'm supper new on the subject and wanted to create an app that persisted data. I have added two new attributes to the model and my app is crashing devastatingly. I'm getting this error and ResearchResult is an object in my model, who's child object ResearchReference now has two new attributes:

Thread 1: Fatal error: Unexpected type for 
CompositeAttribute: Optional<ResearchResult>

What I want to do is to delete all existing data and just rested the whole thing but I can't. I now that I should probably do this, but I don't know where should I put it.

let container = try ModelContainer(for: schema, configurations: [modelConfiguration])
            
let modelContext = ModelContext(container)
try modelContext.delete(model: ResearchObject.self)

I'm wondering if there is a developer tool in Xcode to do this, but so far I have not found anything. Can you please guide me to what should I do?


Solution

  • Add the following launch argument to the scheme you use in Xcode to run the app,

    -com.apple.CoreData.SQLDebug 1 
    

    Then the path to the database file will be printed in the console right at the start when you launch the app. Something like

    "/Users/<username>/Library/Containers/<bundle identifier>/Data/Library/Application Support/default.store"

    Go to that path using Finder or the Terminal app and remove the database file(s), the name is default.store unless you have configured it to be something else.

    There might also be two other temporary files with the same name but the extensions are .store-shm and .store-wal that I also delete as well when I need to trash the database.

    Then the next time you run the app SwiftData will generate new files for you.