Wednesday, February 7, 2024
HomeiOS Developmentios - SwiftData Mannequin not updating values supplied in init

ios – SwiftData Mannequin not updating values supplied in init


I am trying to take care of a neighborhood copy of server knowledge in my utility and thought I might accomplish this with (the truth is, I’ve adopted this text about it within the official Apple Documentation). Nonetheless, SwiftData ignores the values set within the @Mannequin initializer and any subsequent makes an attempt to replace the values.

My impression was that: through the use of the @Attribute(.distinctive) worth on the id parameter, any time I created a mannequin with the identical ID as an present one, SwiftData would replace the present mannequin. That does not seem like the case.

I can not fairly decide why SwiftData just isn’t permitting me to replace my mannequin (esp. from contained in the mannequin’s personal initializer). Is that this a bug? Some undocumented habits? Or have I simply acquired issues configured incorrectly, or am utilizing these fashions in an unsupported method?


This habits might be replicated utilizing a easy mannequin object and initializer like this:

@Mannequin
ultimate class Merchandise {
    
    @Attribute(.distinctive)
    var id: String
    
    var timestamp: Date
    
    var deleted: Bool
    
    // Initializes a neighborhood SwiftData mannequin from a "RemoteItem", which is only a Codable
    // object generated from a server response.
    //
    init?(from remoteModel: RemoteItem) {
        guard let remoteId = remoteModel.id else { return nil }
        self.id = remoteId
        self.timestamp = remoteModel.timestamp
        
        print("Initialized Merchandise mannequin with deletion worth from distant mannequin: (remoteModel.deleted)")
        // Prints appropriate worth of deleted to the console (e.g. true)

        self.deleted = remoteModel.deleted
        
        print("Initialized Merchandise mannequin with distant deletion. Worth is now: (self.deleted)")
        // Prints INCORRECT worth of deleted to the console (e.g. false)
    }
    
}

Then, in a knowledge supply actor (or different comparable class):

func fetchItems() async throws -> [Item] {
    let someRemoteItems = attempt await networkRequestForRemoteItems()
        
    // Map the distant objects to domestically cached objects, much like the demo
    // supplied by Apple right here: https://developer.apple.com/documentation/swiftdata/maintaining-a-local-copy-of-server-data
    let localItemCopies: [Item] = someRemoteItems.compactMap({ Merchandise(from: $0) })
    
    // "Cache" the distant objects
    for localItem in localItemCopies {
        await cacheContainer.insert(localItem)
    }

    return localItemCopies
}

As famous above, any objects that already existed domestically within the SwiftData mannequin retailer are usually not up to date when inserted once more right here. And for added context, I’ve seen different questions asking about comparable habits however solely within the context of SwiftUI, which isn’t related right here.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments