5272f6533f
Fatal error "Duplicate values for key" al construir diccionarios con Dictionary(uniqueKeysWithValues:) sobre dishes/tags/slots cuyos UUID se duplican por la sincronización CloudKit de 2.0. Se sustituyen todas las ocurrencias por Dictionary(_, uniquingKeysWith:) para tolerar duplicados (se conserva el primero) en lugar de abortar. La app ya no crashea al abrir con datos duplicados; la deduplicación real queda pendiente aparte. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A3HaWmmtTQ1vdTERtSYU6p
29 lines
1013 B
Swift
29 lines
1013 B
Swift
import Foundation
|
|
|
|
struct FeedbackStore {
|
|
private static let rejectionKey = "com.mealmood.dishRejections"
|
|
|
|
static func rejectionCounts() -> [UUID: Int] {
|
|
guard let data = UserDefaults.standard.data(forKey: rejectionKey),
|
|
let raw = try? JSONDecoder().decode([String: Int].self, from: data)
|
|
else { return [:] }
|
|
return Dictionary(raw.compactMap { k, v in
|
|
UUID(uuidString: k).map { ($0, v) }
|
|
}, uniquingKeysWith: { first, _ in first })
|
|
}
|
|
|
|
static func recordRejection(for dishId: UUID) {
|
|
var raw: [String: Int]
|
|
if let data = UserDefaults.standard.data(forKey: rejectionKey),
|
|
let decoded = try? JSONDecoder().decode([String: Int].self, from: data) {
|
|
raw = decoded
|
|
} else {
|
|
raw = [:]
|
|
}
|
|
raw[dishId.uuidString, default: 0] += 1
|
|
if let encoded = try? JSONEncoder().encode(raw) {
|
|
UserDefaults.standard.set(encoded, forKey: rejectionKey)
|
|
}
|
|
}
|
|
}
|