Post

Replies

Boosts

Views

Activity

Reply to Text output of the text based on a lab from a CoreMLDataModel
Hello Jack, thanks for the feedback. The data model looks like this: I have the folder Data. In this folder are two subfolders: Clothing and Technology. In the Clothes folder there are many individual text files containing T-shirt, pants, socks, cap, etc.. . In technology with the content camera, notebook, battery, etc.... . I have trained it so that if I enter camera with technology is output, or if I enter pants me clothing is output. But I would like to have it so that if I have a category in the app with the name clothing me the different clothes should be output. Because the app should work in such a way that you can create equipment lists and you are given suggestions for the content for each category to help you. As support. When I bag clothes, for example, socks should be suggested to me, or pants. Janik
Mar ’23
Reply to CoreData always takes the same item
I have tested a few things again. And according to the print statements the problem is that the EditSheet gets the wrong id passed and inside the EditSheet everything fits with the ID. item ID for Edit: 2E5B0F87-362B-4E77-8AC0-95EF1ED18AC0 id during the press of the save button at the edit view: 66C8A3FD-0980-44BD-AE7D-8411D7A07A9C id to update: 66C8A3FD-0980-44BD-AE7D-8411D7A07A9C <Item: 0x600001eb87d0> (entity: Item; id: 0x9b04be453a90c036 x-coredata://8BCB76FB-F8F2-43CE-B703-1E6C1BB03811/Item/p11; data: {     id = "66C8A3FD-0980-44BD-AE7D-8411D7A07A9C";     liste =     (     );     name = D;     timestamp = nil; })
Jan ’23
Reply to CoreData always takes the same item
Sorry. import SwiftUI import CoreData import Foundation struct ContentView: View {     @Environment(\.managedObjectContext) private var viewContext     @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.name, ascending: false)],                   animation: .default)          var items: FetchedResults<Item>          @State private var name: String = ""        @State private var showeditsheet = false     var body: some View {         NavigationView {             VStack{                 TextField("Text eingeben", text: $name)                     .padding()                 Button {                     addItem()                 } label: {                     Text("Save")                 }.padding()                 List {                     ForEach(items, id: \.self) { item in                         NavigationLink(destination: ToDo(list:item)) {                             HStack {                                 Text(item.name ?? "Not found")                                 Button {                                     print(item.id)                                     self.showeditsheet = true                                 } label: {                                     Text("Edit")                                 }.buttonStyle(.bordered)                             }                         }                         .sheet(isPresented: $showeditsheet){                             EditSheet(itemId: item.id!, item:item)                         }                     }                     .onDelete(perform: deleteItems)                 }             }         }     }     //MARK: - Hinzufügen der Listen     private func addItem() {         withAnimation {             let newItem = Item(context: viewContext)             newItem.name = name             newItem.id = UUID()             do {                 try viewContext.save()             } catch {                 let nsError = error as NSError                 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")             }         }     }     private func deleteItems(offsets: IndexSet) {         withAnimation {             offsets.map { items[$0] }.forEach(viewContext.delete)             do {                 try viewContext.save()             } catch {                 let nsError = error as NSError                 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")             }         }     } } import SwiftUI import Foundation import CoreData struct EditSheet: View {     @Environment(\.managedObjectContext) private var viewContext     @Environment(\.dismiss) private var dismiss     @State private var name: String = ""     private var item: Item     var itemid: UUID     init(itemId: UUID, item: Item) {         self.item = item         self.itemid = item.id!         self.name = item.name ?? ""     }     var body: some View {         VStack{             TextField("Text eingeben", text: $name)                 .padding()             Button {                 print(item.id)                 update(item: item, itemid: item.id!)                 dismiss()             } label: {                 Text("Save")             }.padding()         }.onAppear{             name = item.name!         }     }     func update(item:Item, itemid id: UUID){         let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Item")         fetchRequest.predicate = NSPredicate(format: "id == %@", id as CVarArg)         do {             let results = try viewContext.fetch(fetchRequest)             if results.count > 0 {                 let objectToUpdate = results[0]                 if objectToUpdate.value(forKey: "id") as? UUID == id {                     objectToUpdate.setValue(name, forKey: "name")                     do {                         print (id)                         print(objectToUpdate)                         try viewContext.save()                         print("Erfolgreich gespeichert!")                     } catch let error as NSError {                         print("Fehler beim Speichern. \(error), \(error.userInfo)")                     }                 } else {                     print("Fehler")                 }             } else {                 print("Fehler")             }         } catch let error as NSError {             print("Fehler beim Fetchen. \(error), \(error.userInfo)")         }     } }
Jan ’23
Reply to CoreData always takes the same item
editname is from an earlier version. Here it is without meaning. Id when I press the Edit button on the ContenView: 78AB2468-CA12-4A3B-8C55-67F366FDFE7D Id when I press the Save button on the EditView: BCC1267C-ACF6-4926-9BE8-5C4D8062E108 Id the update func print: BCC1267C-ACF6-4926-9BE8-5C4D8062E108 objecttoupdate: <Item: 0x600001196bc0> (entity: Item; id: 0x81a3114acc5b93d0 x-coredata://8BCB76FB-F8F2-43CE-B703-1E6C1BB03811/Item/p3; data: {     id = "BCC1267C-ACF6-4926-9BE8-5C4D8062E108";     liste =( );     name = Asdfasdf;     timestamp = nil; }) I therefore believe that the update func also works. But the func does not get the ID of the selected object. The first print ID in the ContentView always changes depending on the object. The other IDs are always the same, as if it always selects the same object only. Or does not get the correct ID transmitted.
Jan ’23
Reply to CoreData always takes the same item
Ok import SwiftUI import CoreData import Foundation struct ContentView: View {     @Environment(\.managedObjectContext) private var viewContext          //Abfrage zum Anzeigen     @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.name, ascending: false)],                   animation: .default)                         //Referenz zum Entity Item     var items: FetchedResults<Item>               @State private var name: String = ""          //     Test     @State private var editName = ""          @State private var showeditsheet = false                           var body: some View {         NavigationView {             VStack{                 TextField("Text eingeben", text: $name)                     .padding()                 Button {                                          addItem()                                      } label: {                     Text("Save")                 }.padding()                                  List {                     ForEach(items, id: \.self) { item in                                                  NavigationLink(destination: ToDo(list:item)) {                             HStack {                                 Text(item.name ?? "Not found")                                 Button {                                                                          self.showeditsheet = true                                 } label: {                                     Text("Edit")                                 }.buttonStyle(.bordered)                             }                                                                                   }                         .sheet(isPresented: $showeditsheet){                             EditSheet(itemId: item.id!, item:item)                         }                                              }                     .onDelete(perform: deleteItems)                 }             }         }              }          //MARK: - Hinzufügen der Listen     private func addItem() {         withAnimation {             let newItem = Item(context: viewContext)             newItem.name = name             newItem.id = UUID()                          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 { items[$0] }.forEach(viewContext.delete)                          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)")             }         }     } } import SwiftUI import Foundation import CoreData struct EditSheet: View {     @Environment(\.managedObjectContext) private var viewContext     @Environment(\.dismiss) private var dismiss          @State private var name: String = ""     private var item: Item     var itemid: UUID               init(itemId: UUID, item: Item) {         self.item = item         self.itemid = item.id!         self.name = item.name ?? ""     }               var body: some View {         VStack{             TextField("Text eingeben", text: $name)                 .padding()             Button {                                  update(item: item, itemid: item.id!)                 dismiss()                                               } label: {                 Text("Save")             }.padding()         }.onAppear{             name = item.name!                      }     }     func update(item:Item, itemid id: UUID){         let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Item")         fetchRequest.predicate = NSPredicate(format: "id == %@", id as CVarArg)                  do {             let results = try viewContext.fetch(fetchRequest) // Rufe den Fetchrequest auf             if results.count > 0 {                 let objectToUpdate = results[0] // Nehme das erste                 if objectToUpdate.value(forKey: "id") as? UUID == id {                     objectToUpdate.setValue(name, forKey: "name") //Editiere den Key "name" zum übergebenen Wert                                          do {                         try viewContext.save() // Speichern                         print("Erfolgreich gespeichert!")                     } catch let error as NSError {                         print("Fehler beim Speichern. \(error), \(error.userInfo)")                     }                 } else {                     print("Fehler: das aktualisierte Element hat eine andere ID als die übergebene.")                 }             } else {                 print("Fehler: keine Ergebnisse für die angegebene ID gefunden.")             }         } catch let error as NSError {             print("Fehler beim Fetchen. \(error), \(error.userInfo)")         }     }      }
Jan ’23
Reply to CoreData always takes the same item
The ContentView, where the item is set: // //  ContentView.swift //  CoreData_T // //  Created by Janik Hartmann on 31.12.22. // import SwiftUI import CoreData import Foundation struct ContentView: View {     @Environment(\.managedObjectContext) private var viewContext     //Abfrage zum Anzeigen     @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.name, ascending: false)],                   animation: .default)                      //Referenz zum Entity Item     var items: FetchedResults<Item>               @State private var name: String = ""      //     Test     @State private var editName = ""         @State private var showeditsheet = false                      var body: some View {         NavigationView {             VStack{                 TextField("Text eingeben", text: $name)                     .padding()                 Button {                                          addItem()                                      } label: {                     Text("Save")                 }.padding()                 List {                     ForEach(items, id: \.self) { item in                                                  NavigationLink(destination: ToDo(list:item)) {                             HStack {                                 Text(item.name ?? "Not found")                                 Button {                                                                      self.showeditsheet = true                                 } label: {                                     Text("Edit")                                 }.buttonStyle(.bordered)                             }                                                                                   }                         .sheet(isPresented: $showeditsheet){                             EditSheet(itemId: item.id!, item:item)                         }                                           }                     .onDelete(perform: deleteItems)                 }             }             }                   }      //MARK: - Hinzufügen der Listen     private func addItem() {         withAnimation {             let newItem = Item(context: viewContext)             newItem.name = name             newItem.id = UUID()             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 { items[$0] }.forEach(viewContext.delete)             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)")             }         }     } }
Jan ’23
Reply to CoreData always takes the same item
Changed the code again and tried other approaches. Have asked the question again because of this. I load the items from the Item entity. The items are added in the ContentView. Tested with a print-statement that there the Ids are created correctly and also each item has a different one. Only in the EditView it always takes the same ID of the last added item. Also was first the problem that the Ids are randomly selected. Now it always selects the one of the last added item.
Jan ’23
Reply to CoreData takes random id's when editing.
Changed the code again and tried other approaches. Have asked the question again because of this. I load the items from the Item entity. The items are added in the ContentView. Tested with a print-statement that there the Ids are created correctly and also each item has a different one. Only in the EditView it always takes the same ID of the last added item. Also was first the problem that the Ids are randomly selected. Now it always selects the one of the last added item.
Jan ’23