TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Need help with SwiftData

Forums > SwiftUI

@HRait  

I did the tutorial on SwiftData and made a few changes. I have added two attributes to the Sight class because I want to save the contribution and the number in addition to the name. If I only have the name there everything works but as soon as I add another attribute there are problems. I get the error message: Thread 1: Fatal error: failed to find a currently active container for Destination Can anyone help me? Thx

2      

@DaveC  

I'm definitely not an expert, but with the tutorials and experimenting I've been doing, I think it's a problem with migrating the existing data store to the new model. I'm not knowledgeable enough about migrations to tell you how to make it work with the exisiting data store, but if you delete the app completely (which should also delete the data store) and run it again, it should rebuild the store with the new model, and everything thing should work--I think...

3      

There is probably a better way to do this, and actually migrate the old data. But what I've been doing since I have just been working with new apps in simulators so far is this.

In your main app file, add an initializer like this...

import SwiftData
import SwiftUI

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: MyDataType.self)
    }

    init() {
        print(URL.applicationSupportDirectory.path(percentEncoded: false))
    }
}

Then, when you run your app, it will print a line that tells you the location where your database files are stored. You can go to that location in finder, and you should find 3 files there. (default.store, default.store-shm, and default.store-wal)

If you delete all 3 of those files, they will automatically regenerate when you run your app again. However, your data will be completely deleted, and you will start with a fresh empty database. But it has allowed me to work around errors like this while I'm still in the beginning stages of making my app.

3      

@HRait  

Thanks Guys, Sometimes you get stuck with the simplest things 😂

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.