iOS Databases Module 3 Lesson 5 - SwiftData not saving

My DB-project-tracker app is not saving the swift data.
I also downloaded the solution file for Lesson 5, and that also is not saving and retrieving the swift data.
Have tried a range of solutions (with ChatGPTs help) - e.g. cleaning the Xcode build, resetting the simulator but without success. Even managed to find the default.store files and delete these but no luck.
Suggestions please.

Hi Gracie! I’m seeing the same thing with SwiftData in the tutorial. I’m also getting an error with the @Query macro in the console (Though the query is returning the data as expected in the app)

What I suspect it might be, is that the autosave behavior is triggered under a few circumstances and when we refresh the simulator, the things that trigger the autosave to "
save" might not have happened (app in the background, app in the foreground + finishing a run loop)

When I

  1. updated the data in my app
  2. manually selected home in the simulator (Device → Home)
  3. reopened the app

I was able to see the data persist. It seems like this behavior is specific to debug mode but I haven’t tested past the simulator. I think disabling auto save and doing it manually after each call might work too. It might be worth trying to see if that’s the case. If you found a different way around it lmk! I’m seeing the behavior too.

@Query Macro error if anyone is curious.
error: the replacement path doesn't exist: "/var/folders/0f/kzywkzq16l719q802b619b_m0000gn/T/swift-generated-sources/@__swiftmacro_14swiftData_demo11ContentViewV5items33_A91AFBF1ED830BC3C23799003E9F576ELL5QueryfMa_.swift"

I found a forum that goes over a similar issue linked here: Hacking with swift

This is the version of xcode + simulator that I’m running if anyone is curious
XCode: Version 16.2 (16C5032a)
Simulator: Version 16.0 (1038)

@GracieM
Hi Gracie,

I have checked the Lesson 5 code after downloading it from the Dropbox link. I noticed that SwiftData is inconsistent when the first project is created but from then on it seems OK. I also noted that in my final version of the project (after completing the course) that the Button action code includes a directive to force a save. IE:

Button(isEditMode ? "Save" : "Add") {

    if isEditMode {
        project.name = projectName
    } else {
        withAnimation {
            // Add project to SwiftData
            project.name = projectName
            context.insert(project)

            // Force a SwiftData update to make sure that the save has occurred before stats are updated.
            try? context.save()
        }
    }
    //  Dismiss the sheet
    dismiss()
}

Without watching all the subsequent videos again I can’t remember if this was an addition that Chris Ching implemented or if it was something I noticed and made that change to ensure that the data was saved.

In the final version there is an explicit try? context.save() in the EditUpdateView Button code too.

(edit)
I also downloaded the final version from the Dropbox link that Chris Ching provides and that too does not initially save the project when first created. So adding the code:
try? context.save() to the Button action in EditProjectView
solves that issue.
The fact that my version contains that explicit save() points to me having noticed that there was some inconsistency when I followed the build back in December of 2023.

Hi Chris P, thanks for your help with this. The issue resolved as I was completing the database modules and believe it was the update as you suggested above which was added when we updated the EditProjectView. I noticed that if the database saving seemed to be playing up, either closing the app from the simulator or deleting the app then running the build again seemed to trigger resolving the issue as well.