ios – When utilizing Core Data, when should we create a model using xcdatamodeld vs. using a struct?


There are some introductory tutorials to Core Data that creates models by creating an xcdatamodeld file. For example, creating a Book entity with attributes such as title, author, rating, review attributes.

On the other hand, I’ve seen a tutorial where the model is created directly inside a struct. For example, creating a Coin struct with variables such as symbol, name, currentPrice, marketCap properties. There is no xcdatamodeld file.

The main difference I notice is where data comes from:

  • In the Book example (xcdatamodeld), the data is coming entirely from the user manually entering it into the app.
  • In the Coin example (struct), the data is coming from an API, where it decodes based off the model. When users add specific coins to their portfolio, these added coins are stored in Core Data.

I’m not sure if source of data is the determining factor or whether it just comes down to our own preference. Could someone share some insight?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img