I have an entity named Score , which represent a day wise score. This entity has two properties date: Double
and score: Double
. Here date is the midnight timestamp epoch of a day.
I've followed this stackoverflow answer to add date as a unique constraint to this entity.
So my code looks like this:
let dispatchGroup = DispatchGroup()
var factor1: Double = 0
var factor2: Double = 0
var factor3: Double = 0
dispatchGroup.enter()
calculateFactor1(date: date) { score in
factor1 = score
dispatchGroup.leave()
}
dispatchGroup.enter()
calculateFactor2(date: date) { score in
factor2 = score
dispatchGroup.leave()
}
dispatchGroup.enter()
calculateFactor3(date: date) { score in
factor3 = score
dispatchGroup.leave()
}
dispatchGroup.notify(queue: .main) {
let mainScore = factor1 + factor2 + factor3
let scoreObject = Score(context: self.context)
scoreObject.date = date
scoreObject.score = mainScore
do {
try self.context.save()
completion()
}
catch {
completion()
}
}
but this code crashes on context.save
with an error:
Constraint unique violation: UNIQUE constraint failed: ZSCORE.ZDATE
How can I debug this?