UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

an swiftData update issue

Forums > SwiftUI

I have a Classification class

final class Classification: Identifiable, Equatable {
    var id: UUID
    var image: String
    var background: Int
    var name: String
    var hasDeleted: Bool
    var notShow: Bool

    init(id: UUID = UUID(), image: String, background: Int, name: String, hasDeleted: Bool = false,notShow: Bool = false ) {
        self.id = id
        self.image = image
        self.background = background
        self.name = name
        self.hasDeleted = hasDeleted
        self.notShow = notShow

    static let mockData = Classification(image: "questionmark", background: 0xFF0000, name: "请选择标签", hasDeleted: false)

And I have a TodoItem class which hava a attribute type Classification

final class TodoItem: Identifiable {
    var id: UUID
    var title: String
    var startTime: Date
    var endTime: Date
    var isCompleted: Bool
    var isLog: Bool
    var classification: Classification?

    init(id: UUID = UUID(),  title: String, startTime: Date, endTime: Date, isCompleted: Bool, isLog: Bool, classification: Classification?) {
        self.id = id
        self.title = title
        self.startTime = startTime
        self.endTime = endTime
        self.isCompleted = isCompleted
        self.isLog = isLog
        self.classification = classification

    // 创建 Log
    convenience init(title: String, startTime: Date, endTime: Date, classification: Classification?) {
        self.init(id: UUID(), title: title, startTime: startTime, endTime: endTime, isCompleted: true, isLog: true, classification: classification)

    static let mockDataTodo = TodoItem( title: "todo测试", startTime: Date(), endTime: Date(timeIntervalSinceNow: 3000), isCompleted: false, isLog: false, classification: nil)
    static let mockDataLog = TodoItem(title: "log测试", startTime: Date(timeIntervalSinceNow: -3000), endTime: Date(), classification: nil)

now A View show TodoItem data, B View show Classification data , A and B View in TabView. When I update Classification data in B View , A View will not update automatically and you must re-open the app before it will update.

// B View update data
@Environment(\.dismiss) var dismiss
    @Bindable var editTagInfo: Classification
    var modelContext: ModelContext
    let presetColors: [Int] = [0xFF0000, 0x00FF00, 0x0000FF]
    @State private var isSFPresented = false
    @State private var showDeleteAlert = false
    init(classificationId: PersistentIdentifier, in container: ModelContainer) {
        modelContext = ModelContext(container)
        modelContext.autosaveEnabled = false
        editTagInfo = modelContext.model(for: classificationId) as? Classification ?? Classification.mockData

HStack {
                    ColorPicker("tag color", selection: colorValueBinding, supportsOpacity: false)

CircleButton(imageString: "checkmark", width: 35, backgroundColor: .green, imageColor: .white, ifGradient: true)
                        .onTapGesture {
                            withAnimation {
                                try! modelContext.save()


But how do you fetch your data in View A? If you add something in View B, you have to use either @Query which will automatically fetch new data from context, or run fetch to get the data from context.

Also try to add explicit relationship to class Classification

@Relationship(reverse: \TodoItem.classification) var todoItems: [TodoItem] = []

in init you can default it to emtpy array


in todoItems I used @Query to get data, and I added @Relationship , but still the existed todoItems won't change automatically ... And if I reopen app , the existed todoItem will change And after I changed classification , I create a new todoItem, It will follow my change too


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.