I'm seeing this error while using Swift Data.

I understand it's an underlying error from swift data or cloud kit sync, but I don't know how I am supposed to correct it.

The code works correctly! the saving, the sync, but the error is on the log.

I'm almost sure the problem is due to the sync part of SwiftData with iCloud, because if I try this in the simulator, and I am logged, the error messages are thrown but if I'm not logged in on app store/iCloud, the log is clean.

My model uses:

  • UUID
  • Date
  • Color
  • Enum
  • String
  • Dictionaries: [DateComponents:[CustomClasses]]

I think everything is Codable, Identifiable, Hashable.

A little example:

import Foundation
import SwiftData
import SwiftUI

@Model
class Item: Identifiable {
    var id = UUID()
    var creationDate: Date = Date()
    var name: String = ""
    var color: ColorPicked = .mint
    @Relationship(deleteRule: .cascade) var actions: [DateComponents:[Action]] = [:]
    @Relationship(deleteRule: .cascade) var charts: [Chart]? = []
    
    init(id: UUID = UUID(), creationDate: Date, name: String, color: ColorPicked, actions: [DateComponents : [Action]], charts: [Chart]? = nil) {
        self.id = id
        self.creationDate = creationDate
        self.name = name
        self.color = color
        self.actions = actions
        self.charts = charts
    }
}

enum ColorPicked: Codable, CaseIterable, Identifiable {
    var id: String { return self.bgColor.description }
    case white
    case red
    case mint

    var bgColor: Color {
        switch self {
        case .white: return .white
        case .red: return .red
        case .mint: return .betterMint
        }
    }
    
    var textColor: Color {
        switch self {
        case .white: return .darkerGray
        default: return .white
        }
    }
}

class Action: Codable, Identifiable, Hashable, Observable {
    
    enum CodingKeys: CodingKey {
        case id, startDate, duration, amount
    }
    static func == (lhs: Action, rhs: Action) -> Bool {
        lhs.startDate == rhs.startDate && lhs.duration == rhs.duration && lhs.id == rhs.id && lhs.amount == rhs.amount
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
        hasher.combine(startDate)
        hasher.combine(duration)
    }
    
    init(side: Side = .nonSelected, start: Date = Date(), duration: TimeInterval = 0, note: String = "", length: String = "", amount: Volume = Volume(ml: 0.0)) {
        self.startDate = start
        self.duration = duration
        self.amount = amount
    }
    
    var id = UUID()
    var startDate: Date
    var duration: TimeInterval
    var amount: Volume?
    
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.id = try container.decode(UUID.self, forKey: .id)
        self.startDate = try container.decode(Date.self, forKey: .startDate)
        self.duration = try container.decode(TimeInterval.self, forKey: .duration)
        self.amount = try container.decode(Volume.self, forKey: .amount)
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(startDate, forKey: .startDate)
        try container.encode(duration, forKey: .duration)
        try container.encode(amount, forKey: .amount)
    }
}

struct Volume: Codable, Equatable {
    var ml: Double {
        didSet{
            self.oz = ml / 29.574
        }
    }
    var oz: Double {
        didSet{
            self.ml = oz * 29.574
        }
    }
    
    init(ml: Double) {
        self.ml = ml
        self.oz = ml / 29.574
    }
    
    init(oz: Double){
        self.oz = oz
        self.ml = oz * 29.574
    }
}


class Chart: Codable, Identifiable, Hashable, Observable {
    
    enum CodingKeys: CodingKey {
        case id, startDate, duration
    }
    static func == (lhs: Chart, rhs: Chart) -> Bool {
        lhs.startDate == rhs.startDate && lhs.duration == rhs.duration && lhs.id == rhs.id
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
        hasher.combine(startDate)
        hasher.combine(duration)
    }
    
    var id = UUID()
    var startDate: Date
    var duration: TimeInterval
}
0

There are 0 best solutions below