HomeiOS Developmentios - How do I delete baby gadgets from checklist with swiftData?

ios – How do I delete baby gadgets from checklist with swiftData?


I am new to SwiftUI and for my first app I made a decision to attempt SwiftData since I do not wish to should convert it to swiftData later. my app has timers (MyTimer) and every timer has a reputation and a number of other a number of resets which every have a date and a textual content string. I need the consumer to have the ability to choose a MyTimer and think about the checklist of its resets then from there have the ability to edit or delete any of them. so my first view permits the creation of a MyTimer and so as to add resets to it. the second view exhibits an inventory of the resets for the MyTimer chosen. The issue I’ve is once I delete a couple of reset, the array of resets does not appear to replace and in case you delete a second reset from the identical line of the checklist it tries to delete a reset which has already been deleted and I get the error: Thread 1: Deadly error: This technique is not ready to function on a backing knowledge that is unmoored from its managed object context. Relationships will have to be deep copied by objectID for that to work.

that is the code for the app Information Mannequin file:

import Basis
import SwiftData
//import SwiftUI

@Mannequin
closing class MyTimer {
    var id: String
    var title: String
    
    @Relationship(deleteRule: .cascade)
    var resets: [Reset]
    
    @Transient
    var sortedResets: [Reset] {
        print(resets.depend)
        return self.resets.sorted { $0.date > $1.date }
    }

    init(_ title: String = "",
         startDate: Date = .now) {

        let id = UUID().uuidString
        self.id = id
        self.title = title
        self.resets = [Reset(text: "Started On", date: startDate)]
    }
}

@Mannequin
class Reset: Identifiable {
    var id: String
    var textual content: String
    var date: Date

    init(textual content: String, date: Date) {
        self.id = UUID().uuidString
        self.textual content = textual content
        self.date = date
    }
}

App File:

import SwiftUI
import SwiftData

@foremost
struct SwiftDataHabitTestApp: App {

    let container = attempt? ModelContainer(for: MyTimer.self, Reset.self)


    var physique: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(container!)
    }
}

ContentView:

import SwiftUI
import SwiftData

struct ContentView: View {

    @Atmosphere(.modelContext) personal var modelContext
    @State var timerName: String = ""
    @State var resetViewShowing = false
    @State var timerForResetView: MyTimer?
    @Question(kind: MyTimer.title) var allTimers: [MyTimer]

    var physique: some View {
        ZStack {
            VStack {
                TextField(textual content: $timerName, immediate: Textual content("Identify your Timer")) {
                    Textual content("Identify:")
                }
                Button {
                    let newTimer = MyTimer(timerName)
                    modelContext.insert(newTimer)
                } label: {
                    Textual content("Create Timer")
                }
                Listing {
                    ForEach(allTimers){ dispTimer in
                        Textual content(dispTimer.title)
                        Button {
                            let newReset = Reset(textual content: "New REset", date: .now)
                            dispTimer.resets.append(newReset)
                            attempt? modelContext.save()
                        } label: {
                            Textual content("Reset")
                        }
                        Button {
                            resetViewShowing = true
                            timerForResetView = dispTimer
                        } label: {
                            Textual content("View Resets")
                        }

                    }
                    .onDelete(carry out: { indexSet in
                        indexSet.forEach { index in
                            modelContext.delete(allTimers[index])
                        }
                    })
                }

            }
            .padding()
            if resetViewShowing {
                ResetListView(dispTimer: timerForResetView!)
            }
        }
    }
}

Reset Listing View

import SwiftUI
import SwiftData

struct ResetListView: View {

    @Atmosphere(.modelContext) personal var modelContext
    @State var dispTimer: MyTimer

    var physique: some View {
//      var resetList: [Reset] = dispTimer.resets.sorted { $0.date > $1.date }
        Listing {
            ForEach(dispTimer.resets) {
                Textual content($0.textual content)
            }
            .onDelete(carry out: { indexSet in
                indexSet.forEach { index in
                    print("index: (index)")
                    print("size of resets: (dispTimer.resets.depend)")
                    modelContext.delete(dispTimer.resets[index])
                    do {
                        attempt modelContext.save() <<---- Error: Thread 1: Deadly error: This technique is not ready to function on a backing knowledge that is unmoored from its managed object context. Relationships will have to be deep copied by objectID for that to work.
                    } catch {
                        print("error saving")
                    }
                }
            })
        }
    }
}

I’ve tried a number of methods of utilizing a sorted array of resets as I would really like this to show within the checklist so as by date, sorting them right into a var within the view or utilizing a computed property in MyTimer however then I get a unique error: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1a8a4fad8) which seems within the getter for reset’s date property I’ve additionally tried as an alternative of utilizing .ondelete to make use of a .swipeActions however that does not appear to unravel the issue.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments