title: Complex Core data relationship mess
body: <p>need advice on a couple of entities and their relationships and the whole approach to it. Alot of core data tutorials and examples that are out there seem simple after a while when you actually attempt to make something real!..hence the question:</p>
  
  <p>I need to know the approach and if what i've done is okay.. lets say a user creates his profile and starts organizing business cards. from the start, Lets say the app is capable of handling many users. </p>
  
  <p>the Model: 
  Entities:</p>
  
  <p>1-User
  2-CardGroup
  3-Card
  4-Entries</p>
  
  <p>Now these are the four entities.
  User is the list of users added, each holding their own set of cards. THIS IS SIMPLY THE USER USING THE DEVICE(APP) NOT THE LIST OF PEOPLE ACTUAL BUSINESS CARDS ARE BEING STORED. For example, a user would be me, and Ive got a bunch of business cards lying around to organize in this app. this entity 'User' wouldnt not hold the names stamped on the business card,.. hope you get the point.</p>
  
  <p>the entity Card, holds a list of business cards. This is the main one that actually holds the data to the card.</p>
  
  <p>CardGroup, this is sorta the smart groups. A user can add categories to this like "Wallstreet" as one group, another could be "Silicon valley", their could be "UniversityMen" or just "Laydees".</p>
  
  <p>Entries are This is kinda like a duplicates of a Card, lets say that guy had 5 different jobs over time, this entry just holds those aspects of the card that changed over time. A tracker so to speak..</p>
  
  <p>I want to figure out the relationships here..</p>
  
  <p>1- User &lt;----------------->>CardGroup
  A user can have multiple groups for cards - Wallstreet, SiliconValley, MyCity, WorkPlace</p>
  
  <p>2- CardGroup &lt;------------>>CardDude
  Each cardGroup can hold a bunch of biz cards.
  A CardDude can be a part of WallStreet and/or MyCity, which is saying the same..?
  Also, Wallstreet can hold many cards.
  Im confused whther this is a many-to-many relationship or a one-to-many
  CardGroup &lt;&lt;------------->> CardDude</p>
  
  <p>THis is making me realise that english isnt my first language. </p>
  
  <p>3.
  A user can know only One CardDude right? Lets call em Jason, obviously there cant be many kinda Jasons.? Its gotta be just one.
  And cuz of that this relationship is
  User&lt;---------------->CardDude   one-to-one.
  Im not even sure here..</p>
  
  <p>Also, If my dad is another user on this app (with his name in the first entity "User"), then he could potentially have his copy of CardDude Jason. Yea, im willing to do that for a reason. ahem.. So in the end, me and my dad both have their Jasons in CardDude. If i modify Jason as a User, then it should reflect on the other CardGroups like wallstreet, silicon but not on my dad's User entires, that should remain the same ( assuming he created one similar before).</p>
  
  <p>Again, im not sure whats the relationship now..
  User &lt;------------------>CardDude</p>
  
  <p>The pretext here is, when adding a carddude, this should happen only once for the USER and not let the USER add the same CARDUDE more than once, He could if a different USER is adding.</p>
  
  <p>Heres the bigger puzzle, 
  Now I wana add the latest info to that CardDude, 
  Im callin it Entry. All it does is create a timeStamp, And add an entry of CardDude Jason, about a new card he may have had. a new job so to speak.</p>
  
  <p>I'd want to see for every group.. in sql terms,</p>
  
  <p>select ENTRY.name ENTRY WHERE USER = ME AND CARDGROUP = WALLSTREET</p>
  
  <pre><code>      I thot I'd have 
    CardGroup(WallStreet) &lt;-----------&gt;&gt;CardDude (Jason) and some more&lt;-----------&gt;&gt;Entry
  </code></pre>
  
  <p>WHERE, USER(me)&lt;------------>CardDude(Jason)</p>
  
  <pre><code> USER&lt;--------------&gt;&gt;CardGroup to many
    USER&lt;---------------&gt;CardDude (onetoone)
    USER&lt;---------------&gt;&gt;Entry (to many)
  
    CardGroup&lt;------------&gt;&gt;CardDude
    CardDude&lt;-------------&gt;&gt;Entry (tomany) can tell me how many last cards did Jason get..
  </code></pre>
  
  <p>Please guide me on how to achieve a clear picture here, also Since Each USER can have only ONE Relationship with CardDude, how would i avoid putting in a duplicate similar entry..
  Another point is, my dad's user would recreate the whole cardgroups, carddude and entries. they're quite independent.</p>
  
  <p>THANKS GUYS, THIS is a looong question..</p>
  
  <p><strong>UPDATE</strong>: after the answer, I understood a lot of things, Now one thing remains, its they ideal way to get from CardGroup to CardEntry (which will include all CardPerson's) in that CardGroup... Right now Im just iterating through, but theres gotta be a better way to get all those CardEntries.</p>
  
  <pre><code> //cardGroupFriends  is a CardGroup object        
    for(CardPerson *person in [cardGroupFriends persons]){
               for(CardEntry *entry  in [person entries]){
                   NSLog(@"%@ = %@", person.name, entry.company);
               }
           }
  </code></pre>
  
  <p>I just can't help but approach this via the sqlite route.. but I'm getting to like core data! It actually made things pretty simple., 
  I was thinking about NSFetchedResultsController to fetch CardEntries from CardGroups, bypassing CardPerson entity (assuming I'm fetching all CardPersons in CardGroups) and fetch CardEntries using a timeStamp as sort order. </p>
  
  <p>And for my understanding.. when Im adding a new CardEntry, I'm just using </p>
  
  <pre><code>[cardPerson addCardEntryObject:newEntry];
  </code></pre>
  
  <p>Is it understood that this "newEntry" is part CardPerson-->then CardGroup-->Current -User?? Assuming a User was selected at app launch and cardGroups are derived from there? Does core data take care of that? I don't want this newEntry to show up  when the app switched User and selected the same CardPerson!
  Really appreciate your help</p>