Changes to SwiftData SubModels do not update the UI

Hello, I'm having a problem with SwiftData that I can't find a solution for. Every change made to the firstArray updates the interface. However, the changes I make on secondArray do not update the interface. The changes on secondArray are saved but the UI is not updated. (I can see that the interface is updated by the text giving the number of elements in the array. )

Model:

import Foundation
import SwiftData

@Model
final class Item {
    var firstArray: [String]
    var itemDetail: ItemDetail
    
    init(firstArray: [String], itemDetail: ItemDetail) {
        self.firstArray = firstArray
        self.itemDetail = itemDetail
    }
}

@Model
class ItemDetail {
    var secondArray: [String]
    
    init(secondArray: [String]) {
        self.secondArray = secondArray
    }
}

Content View:

import SwiftUI
import SwiftData

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

    var body: some View {
        NavigationStack {
            List {
                ForEach(items) { item in
                    NavigationLink("Item") {
                        ItemDetailView(item: item, itemDetail: item.itemDetail)
                    }
                }
            }
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Button("Add", systemImage: "plus") {
                        let item = Item(firstArray: ["Mehmet", "Fırat", "Şirin"], itemDetail: ItemDetail(secondArray: ["Mehmet", "Fırat", "Şirin"]))
                        modelContext.insert(item)
                    }
                }
                ToolbarItem(placement: .topBarLeading) {
                    Button("Delete", systemImage: "trash", role: .destructive) {
                        try? modelContext.delete(model: Item.self)
                    }
                    .tint(.red)
                }
            }
        }
    }
}

Detal View:

import SwiftUI

struct ItemDetailView: View {
    
    let item: Item
    let itemDetail: ItemDetail // for Version 3
    
    var body: some View {
        List {
            //MARK: Version 1
            
            Text(item.firstArray.count.formatted())
            
            Section {
                ForEach(item.firstArray, id: \.self) {
                    Text($0)
                }
                .onDelete {
                    item.firstArray.remove(atOffsets: $0)
                }
            }
            
            //MARK: Version 2
            
            Text(item.itemDetail.secondArray.count.formatted())
            
            Section {
                ForEach(item.itemDetail.secondArray, id: \.self) {
                    Text($0)
                }
                .onDelete {
                    item.itemDetail.secondArray.remove(atOffsets: $0)
                }
            }
            
            Button("Delete All") {
                item.itemDetail.secondArray = []
            }
            
            //MARK: Version 3
            
            Text(itemDetail.secondArray.count.formatted())
            
            Section {
                ForEach(itemDetail.secondArray, id: \.self) {
                    Text($0)
                }
                .onDelete {
                    itemDetail.secondArray.remove(atOffsets: $0)
                }
            }
            
            Button("Delete All") {
                itemDetail.secondArray = []
            }
        }
    }
}

Try the versions with only one on the screen. In version 1 everything works fine. In version 2 the deletion works but the interface is not updated. In version 3 everything works fine. I don't think version 3 is a very correct method so I am waiting for your ideas and solutions. Thanks for all the answers.

Perhaps try defining a relationship in your models. https://developer.apple.com/documentation/swiftdata/defining-data-relationships-with-enumerations-and-model-classes/

Your current usage of these models is non-standard. I understand that this is a toy example, but it isn't normally done to create a Model inside another Model without defining a relationship between them.

You would normally do something like this:

import Foundation
import SwiftData

@Model
final class Item {
    var firstArray: [String]
    var itemDetail: ItemDetail
    
    init(firstArray: [String], itemDetail: ItemDetail) {
        self.firstArray = firstArray
        self.itemDetail = itemDetail
    }
}

@Model
class ItemDetail {
    var secondArray: [String]
    // VVV This is the new line that creates the relationship.
    @Relationship(inverse: \Item.itemDetail) var item: Item
    
    init(secondArray: [String]) {
        self.secondArray = secondArray
    }
}
Changes to SwiftData SubModels do not update the UI
 
 
Q