GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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.

4      

@HRait  

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

2      

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.