Hello I need some more help.
I have a CoreData model named Item, which in turn has some properties like name, price, quantity
The items are store in an array placed in the model of the app
struct PosViewModel {
let persistenceController = PersistenceController.shared
let container: NSPersistentContainer
let viewContext: NSManagedObjectContext
var ticket: [Item] = []
init() {
container = persistenceController.container
viewContext = container.viewContext
let request = NSFetchRequest<Category>(entityName: "Category")
request.sortDescriptors = [NSSortDescriptor(keyPath: \Category.name, ascending: true)]
do {
categories = try viewContext.fetch(request)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
}
When i change the property of the item like
ticket[index].quantity += 1
The view does not update
For now I'm using a very ugly workaround like this:
ticket.append(Item(context: viewContext))
ticket.popLast()
With this in place the view updates cause I add and remove an element from the array
Of course PosViewModel is declared as @Published in my ViewModel class, the other stuff works and the views update normally but not when i change a property inside the coredata item.
Thank you