Hello I have the problem that I can not update items that have already been saved with CoreData. I have tried different ways, but each time came several different error messages. In addition, many instructions are very old or use UIKit. I think I always think of the wrong approach. Do you know one or more ways how I can update existing items? I'll send a code along, though I've removed my attempts for now. So maybe you can give me some approaches based on my code.
Thanks in advance!
The ItemView:
import SwiftUI
import Foundation
import CoreData
struct ToDo: View {
@Environment(\.managedObjectContext) private var viewContext
// Referenz zu Entitiy Item
@ObservedObject var list: Item
//Die Todos mit Referenz zum Array vom Entity ToDos
@State var liste: [ToDos]
@State private var showingNewToDo = false
//Die Funktionen der Store-Klasse
var store = Store()
@State private var todo: String = ""
// Konstruktor-Hack, um die Verwendung
// von ToDos als Status zu ermöglichen (damit die
// Liste aktualisiert wird)
init(list: Item){
self.list = list
self.liste = list.liste?.array as! [ToDos]
}
var body: some View {
NavigationView {
List {
//Zeigt jedes ToDo für die Liste
ForEach(liste.sorted(using: SortDescriptor(\ToDos.todo))) { todo in
Text(todo.todo ?? "Not found")
}
.onDelete(perform: deleteItems)
}
}.toolbar {
EditButton()
Button() {
showingNewToDo.toggle()
} label: {
Label("Hinzufügen", systemImage: "plus")
}
}
.sheet(isPresented: $showingNewToDo){
ToDoAddSheet() { todo in
//speichert Änderungen
list.addToListe(todo)
store.save(viewContext: viewContext)
//Fügt ToDos zu Listen hinzu
withAnimation{
liste.append(todo)
}
}
}
}
//Wird hier nicht benötigt, würde aber Daten speichern
private func addItem() {
withAnimation {
let newToDo = ToDos(context: viewContext)
newToDo.id = UUID()
newToDo.todo = todo
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { liste[$0] }.forEach(viewContext.delete)
liste.remove(atOffsets: offsets)
store.save(viewContext: viewContext)
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}
The AddView:
import SwiftUI
struct ToDoAddSheet: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.managedObjectContext) private var viewContext
var store = Store()
var save: (ToDos) -> ()
@State private var todo: String = ""
var body: some View {
VStack{
TextField("Text eingeben", text: $todo)
.padding()
Button {
add()
dismiss()
} label: {
Text("Save")
}.padding()
}
}
//MARK: - Syntax für neue todos
private func add() {
let newToDo = ToDos(context: viewContext)
newToDo.todo = todo
newToDo.id = UUID()
save(newToDo)
}
}