TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SwiftData and UUIDs Creating Entities that have attributes for other Entities UUIDs and initializing them

Forums > SwiftUI

@delawaremathguy Thanks for the original set of posts/replies on this subject. After re-reading them multiple times I was able to find the way to get nil into an Id of type UUID for another Entity. I have an Entity named "Hour" that will eventually be associated with an "Invoice" Entity but I haven't yet created the "Invoice" so I don't have access to it's Id whichs is an UUID.

Solution: I have an "Hour" attribute named invoiceId that is a UUID? NOTE: the Optional ?. Then using SwiftData's init it creates invoiceId: UUID? = nil So when I create the Hour entity without an Invoice the initializer for the invoiceId is this: invoiceId: nil

Hope this will assit others.

2      

hi Michael,

as i did last time, i'll start my reply: "took a few minutes to remember this ..." it looks like you are referencing this thread.

i'm happy you are good with the idea.

i have two quick comments to add at the moment, as i, too, am exploring SwiftData right now:

  • i don't see that you can have your own implementation of an id property in your SwiftData models. if you have such an attribute now (Core Data seems OK with this), and you want to have SwiftData use the data store you already have, you would first need to migrate your current Core Data entity, renaming the existing id attribute to something else, e.g., a referenceID.

  • however, if Core Data is working with NSPersistentCloudKitContainer, you have a problem: renaming does not work with CloudKit.

thanks for the mention, and as i said, you can find me on GitHub.

DMG

2      

In SwiftData, when creating entities that possess attributes referring to UUIDs of other entities, it's crucial to ensure seamless initialization and management UUIDs, or Universally Unique Identifiers, play a pivotal role in uniquely identifying these entities across the system.

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.