Trouble with Persisting One-to-Many Relationship Data in SwiftData, What am I Missing?

Hi everyone,

I'm new to programming and I've been experimenting with Apple's SwiftData. I've run into an issue I can't seem to resolve.

I'm creating a personal relationship manager app where I have a Relation model and an Interaction model. Relation has a one-to-many relationship with Interaction. I'm using SwiftData's @Model and @Relationship property wrappers to define these models and their relationship. I've taken inspiration from Apple's sample code, that can be found here:

Adopting SwiftData for a Core Data app (WWDC23 Session: "Migrate to SwiftData")

The relevant parts of the models look something like this:

@Model
final class Relation {
    ...
    @Relationship(.cascade, inverse: \Interaction.relation)
    var interactions: [Interaction] = []
    ...
}

@Model
final class Interaction {
    ...
    var relation: Relation?
    ...
}

In my SwiftUI view, I'm adding a new Interaction to a Relation like this:

private func AddItem() {
    withAnimation {
        let newInteraction = Interaction(...)
        modelContext.insert(newInteraction)
        newInteraction.relation = relation
        relation.interactions.append(newInteraction)
    }
}

When I add a new Interaction like this, everything seems to work fine during that app session. I can see the new Interaction in my app's UI. But when I quit the app and relaunch it, the new Interaction is gone. It's as if it was never saved.

I've double-checked my code and as far as I can tell, I'm using SwiftData correctly. My usage aligns with the sample code provided by Apple, and I'm not getting any errors or warnings. I think that this issue is not related to SwiftData being in Beta, because Apple's sample code works perfectly fine.

I have a few questions:

  1. Is there something I'm missing about how to properly save models using SwiftData?
  2. Is there a specific step or method I need to call to persist the changes to the Relation and Interaction objects?
  3. Is there a way to debug what's going wrong when SwiftData attempts to save these changes?

Any help would be greatly appreciated.

Thank you in advance!

Louis

Accepted Reply

Solved it! In case anyone is wondering, the problem occurred because I'm also storing an enum case in my Interaction class, which is a known issue in iOS 17 Beta 2. The workaround was to store a String and then "translate" it to an enum type using a computed property. The computed property is declared as @Transient, so it's not stored. And it works perfectly fine!

Replies

Solved it! In case anyone is wondering, the problem occurred because I'm also storing an enum case in my Interaction class, which is a known issue in iOS 17 Beta 2. The workaround was to store a String and then "translate" it to an enum type using a computed property. The computed property is declared as @Transient, so it's not stored. And it works perfectly fine!

now you need to add "deleteRule:" before the .cascade.