Core Data Modelling

Hi


I've just joined, so this is my first post. I am working on my first project to help me learn Swift/iOS coming from a Python,PHP,SQL,JavaScript background. So Core Data is new to me and having a little problem trying to work out how to do the following in Core Data. I have done a lot of reading and asking questions. This problem doesn't seem this easy in Core Data, maybe I'm bad at explaining... 😐 But here goes, any help greatly appreciated, I'm a newb remember 😁


I have the following app I am trying to build, just a little player tracker for some sport games. I'll show my rough modelling


class Player: NSManagedObject {
// Player has a name and belongs to a team
@NSManaged var name: String
@NSManaged var team: Team?
}


class Team: NSManagedObject {
// Team has a name, Set of Players and Set of Games
@NSManaged var name: String
@NSManaged var players: Set<Player>
@NSManaged var games: Set<Game>
}


class Game: NSManagedObject {
// Game has a Set of Teams, the date and Set of Players who played in the game
@NSManaged var teams: Set<Team>
@NSManaged var date: NSDate
@NSManaged var played: Set<Player>
/*
Here in each Game I had planned to store a dictionary of each Player and how many minutes they'd played so:
<Player A : 65, Player B: 20...>
*/
}


This it seems isn't as easy as I thought and I've asked a few different people on forums and some didn't know how.

So I wondered what is the correct Core Data way to model data like this? I am a newb so might not get everything you say, but will do my best.


Thanks 🙂


Jonny

Have to ask...Core Data assumes several non-trivial skill sets - is your hesitation about asking the right question a reflection of that being an issue for you?


If so, you might not understand replies, etc. and wish to get going with something less intensive, working up to CD at a later date, perhaps.


If, tho, you're secure in this arena, then head on and good luck.


Ken

If you are just starting a project, then make sure you check off the 'Use Core Data' box when you create it. You will see a .xcdatamodeld file in your project. If you are not starting fresh, add the .xcdatamodeld file into your project. Click on the model file to get started entering in your model. You can add an Entity and then add Attributes to the entity. In your example, you would have Player, Team and Game entities with their associated attributes.

If you've done work in PHP and SQL, then I wonder if you learned the wrong lesson as far as composing data structures that are searchable. 😐 Because I assume that's the part where you're getting stuck modeling your game data structure.


The basic rules of Core Data modeling are:

0. Never assume your data fits in memory. If your data fits in memory (or you don't care), Core Data is overkill and you're wasting development time.

1. Only Core Data entities can reference other Core Data entities. They must do this using relationships.

2. Non-Core Data objects (including collections such as dictionaries) cannot reference Core Data entities directly.

The first rule is part of the nature of the system. The second rule you can try to fight, but if you try to fight it you punish yourself and work against the design of the system. If you're new to Core Data and you find yourself violating the second rule, you're using the wrong approach.


Your game time dictionary violates rule #2, and partially violates rule 0.


One of the conforming representations would be to define an intermediate entity:

+ relationship playerReference -> Player Entity

+ relationship gameReference -> Game Entity

+ attribute time

and define inverse relationships on Game Entity and Player Entity. Depending on how much SQL work you've done, and who you've done it for, you may recognize that sort of intermediate structure.


If you have a Player Entity reference, you can generate a fetch request to give the {Player Entity.name, Game Entity.name, time} tuples. This is left as an exercise for the reader, but it's one of the reasons for modeling the game time entity that way.

Core Data Modelling
 
 
Q