Since I am coming from those programmers who have used sqlite extensively, perhaps I am just having a hard time grasping how Core Data manages to-many relationships.
For my game I have a simple database schema on paper.
Entity: Level - this will be the table that has all information about each game level
Attributes: levelNumber(String), simply the level number
: levelTime(String), the amount of time you have to finish the level(this time will vary with the different levels)
: levelContent(String), a list of items for that level(and that level only) separated by commas
: levelMapping(String), how the content is layed out(specific for a unique level)
So basically in core data i want to set up the database so i can say in my fetchRequest:
Give me the levelTime, levelContent and levelMapping for Level 1.(or
whatever level i want)
How would i set up my relationships so that i can make this type of fetchRequest?
Also I already have all the data ready and know what it is in advance. Is there any way to populate the entity and its attributes within XCode?
As you’ve described it, it’s a single Core Data entity, called
Levelthat has four string attributes. Since there’s just the one entity, there are no relationships. You’d create the one entity and add properties so that it looks just like you’ve described it above:Getting just one
Levelis basic Core Data fetching:If it was me I’d use one of the numeric types for
levelNumber, but maybe you have some reason to use a string there. I’d also probably breaklevelContentinto a separate entity, because (a) comma delimited strings are ugly, no matter how you slice ’em, and (b) you might well want the items to have more attributes, and a separate entity would hold those.