When performing a delete action, I am getting this error, and there seems to be no fix for it.
Here is my Model :
@Model
class Expense : Identifiable {
var entityId : String
var amount: Double
var category : ExpenseCategory
var desc: String?
var date: Date?
var associatedBudget : Budget?
init(id: String, amount: Double, category: ExpenseCategory, description: String? = nil, date: Date? = nil) {
self.entityId = id
self.amount = amount
self.category = category
self.desc = description
self.date = date
}
}
ExpenseCategory is just an enum
Here are two approaches I tried
func deleteExpense(expense: Expense) -> Bool {
let context = DatabaseHelper.shared.getModelContext()
context.delete(expense)
return true
}
func deleteExpense(expense: Expense) -> Bool {
let context = DatabaseHelper.shared.getModelContext()
let expenseId = expense.entityId
let predicate = #Predicate<Expense> { $0.entityId == expenseId}
let fetchDescriptor = FetchDescriptor<Expense>(predicate: predicate)
do {
let items = try context.fetch(fetchDescriptor)
for item in items {
context.delete(item)
}
try context.save()
return true
}
catch {
Logger.log(.error, error.localizedDescription)
return false
}
}
In both the cases I get the same result.