London Escorts sunderland escorts 1v1.lol unblocked yohoho 76 https://www.symbaloo.com/mix/yohoho?lang=EN yohoho https://www.symbaloo.com/mix/agariounblockedpvp https://yohoho-io.app/ https://www.symbaloo.com/mix/agariounblockedschool1?lang=EN
2 C
New York
Friday, January 31, 2025

ios – SwiftData: including a UUID to an present mannequin at all times creates the identical id


I’ve an issue updating a SwiftData mannequin by including a property.
Particularly, I’ve the next mannequin and I’m making an attempt to replace it by including an id. The issue is that, if I do that, all information already saved within the app get up to date with the identical UUID. Clearly I want to have a unique id for each occasion. Am I lacking one thing? Ought to I do one thing otherwise?

Phrase mannequin

Within the earlier model the mannequin didn’t have the id property, the corresponding coding key and attribute within the initializer.


@Mannequin
class Phrase: Codable {
    enum CodingKeys: CodingKey {
        case id, time period, learntOn, notes, class, bookmarked
    }
    
    var id: UUID = UUID()    // marked it as var for testing
    let time period: String = ""
    let learntOn: Date = Date.now
    var notes: String = ""
    @Relationship(inverse: Class.phrases) var class: Class?
    var bookmarked: Bool = false
    
    init(id: UUID, time period: String, learntOn: Date, notes: String = "", class: Class? = nil, bookmarked: Bool = false) {
        self.id = id
        self.time period = time period
        self.learntOn = learntOn
        self.notes = notes
        self.class = class
        self.bookmarked = bookmarked
    }
    
    required init(from decoder: Decoder) throws {
        let container = strive decoder.container(keyedBy: CodingKeys.self)
        self.id = strive container.decode(UUID.self, forKey: .id)
        self.time period = strive container.decode(String.self, forKey: .time period)
        let learntOn = strive container.decode(String.self, forKey: .learntOn)
        self.learntOn = strive Date(learntOn, technique: .iso8601)
        self.notes = strive container.decode(String.self, forKey: .notes)
        self.class = strive container.decode(Class?.self, forKey: .class)
        self.bookmarked = strive container.decode(Bool.self, forKey: .bookmarked)
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        strive container.encode(self.id, forKey: .id)
        strive container.encode(self.time period, forKey: .time period)
        strive container.encode(self.learntOn, forKey: .learntOn)
        strive container.encode(self.notes, forKey: .notes)
        strive container.encode(self.class, forKey: .class)
        strive container.encode(self.bookmarked, forKey: .bookmarked)
    }
    
    static func decodeWords(from json: String) throws -> [Word] {
        let knowledge = json.knowledge(utilizing: .utf8)!
        let decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .iso8601
        return strive decoder.decode([Word].self, from: knowledge)
    }
    
    static let instance = Phrase(id: UUID(), time period: "Swift", learntOn: .now, notes: "A swift testing phrase.", bookmarked: true)

Related Articles

Social Media Auto Publish Powered By : XYZScripts.com