So I’m saving Day Objects using SwiftData.
@Model
class Day: Hashable {
let id : UUID
let date: Date
var percentageCompleted: Double
var completedHabitIDs: Set<UUID>
init(id: UUID, date: Date, percentageCompleted: Double, completedHabitIDs: Set<UUID>) {
self.id = id
self.date = date
self.percentageCompleted = percentageCompleted
self.completedHabitIDs = completedHabitIDs
}
}
There’s a function that deletes all user data
// Environment Variables
@Environment(\.modelContext) private var context
private func eraseData() {
do {
try context.delete(model: Day.self)
try context.delete(model: Habit.self)
try Auth.auth().signOut()
gvm.onlineSync = false
gvm.currentUser = nil
onlineSyncToggle = false
try context.save()
deleted()
} catch {
print("Failed to Erasa Data")
}
}
Right After I call the eraseData
function, I’m trying to save a dayObject for today because it was just deleting and the app needs a dayObject.
private func saveTodOrDay(day: Day? = nil) -> Day {
let dayObj = day != nil ? day! : vm.createEmptyDateObj(for: Date())
context.insert(dayObj)
return dayObj
}
The Catch block of the eraseData
doesn’t get executed therefore no errors are thrown, the problem is here in the context.insert(dayObj)
line.
Thread 17: EXC_BAD_ACCESS (code=1, address=0x8000000000000008)
That’s the error I got when i ran in the simulator
Why is that? and how can I fix it?