SwiftData Relationship Persistence

I'm still getting started with SwiftData and having trouble understanding how to persist a model with a relationship in a container. I've tried to distill the example below to be pretty simple.

Here are some example model definitions. I would like to be able to define a n Item. That Item can have SubItems related to it in a one-to-many fashion. Each SubItem is required to be attached to a parent Item.

final class Item {
    var name: String
    var subitems: [Item]?
    
    init(
        name: String,
        subitems: [SubItem]? = nil
    ) {
        self.name = name
    }
}

@Model
final class SubItem {
    var name: String
    
    init(name: String) {
        self.name = name
    }
}

In my app I am then defining a preview container with some pre-saved data already.

    Item(
        name: "item1",
        subitems: [
            SubItem(name: "subItemA"),
            SubItem(name: "subItemB")
        ]
    ),
    Item(
        name: "item2",
        subitems: [
            SubItem(name: "subItemC"),
            SubItem(name: "subItemD")
        ]
    )
]


@MainActor
let PreviewContainer: ModelContainer = {
    do {
        let schema = Schema([
            Item.self,
            SubItem.self,
        ])
        
        let container = try ModelContainer(
            for: schema, configurations: ModelConfiguration(isStoredInMemoryOnly: true)
        )
                
        for item in PreviewItems {
            container.mainContext.insert(item)
            
            for subItem in item.subitems! {
                container.mainContext.insert(subItem)
            }
        }
                
        return container
    } catch {
        fatalError("Failed to create container")
    }
}()

My app is then defined as follows...

struct SwiftDataTestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(PreviewContainer)
    }
}

struct ContentView: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var items: [Item]

    var body: some View {
        HStack {
            VStack {
                Text(items[0].name)
                Text(String(items[0].subitems!.count))
//                Text(items[0].subitems![0].name)
//                Text(items[0].subitems![1].name)
            }
            
            Spacer()
            
            VStack {
                Text(items[1].name)
                Text(String(items[1].subitems!.count))
//                Text(items[0].subitems![0].name)
//                Text(items[0].subitems![1].name)
            }
        }
        .padding(100)
    }
}

#Preview {
    ContentView()
        .modelContainer(PreviewContainer)
}

The preview loads without an issue, but if I uncomment the lines that access the SubItems it crashes. In the preview I can also see that each Item has 0 SubItems related to it. For some reason, the model container is not actually storing the `SubItem even though they are defined in PreviewItems.

Some things I've tried

  • Explicitly adding the relationship
  • Adding a Item property in SubItem to link back to it's parent
  • In the PreviewContainer definition, manually insert the SubItems as well as the parent Items

Any help is appreciated, thanks

Answered by joadan in 803221022

It doesn't work well when all objects you want to add are new and haven't been inserted before. Try to first insert the Item object before adding the SubItem objects.

For example

let item = Item(name: "Item1", subitems: [])
container.mainContext.insert(item)

item.subitems.append(SubItem(name: "subItemA"))
item.subitems.append(SubItem(name: "subItemB"))
Accepted Answer

It doesn't work well when all objects you want to add are new and haven't been inserted before. Try to first insert the Item object before adding the SubItem objects.

For example

let item = Item(name: "Item1", subitems: [])
container.mainContext.insert(item)

item.subitems.append(SubItem(name: "subItemA"))
item.subitems.append(SubItem(name: "subItemB"))
SwiftData Relationship Persistence
 
 
Q