Search code examples
objective-ccore-dataentity-relationshipone-to-manyone-to-one

Trying to understanding Core Data relationships or handling duplicates


I'm trying to verify I understand Core Data relationships and/or possibly how to handle duplicates.

In my example below, I have entity Account in a to-many relationship with entity Transaction Date. I'm thinking to-many as there will be multiple dates under the one Account.

Where I'm getting confused is that I only want to have one specific date... meaning, only one date and no duplicates. The intent is then to have entity Transaction Date to have a to-many relation with entity Event. So account XYZ will have trans date of 06/11/2012 and multiple entries for entity Event. Then account XYZ will have trans date of 06/12/2012 and multiple entries for entity Event.

Is the relationship between Account and Trans Date truly a to-many or to-one? If it is a to-many... how are duplicates handled? How do I keep only one date in entity Trans Date? If my code is adding by entries into Event and Trans Date, is some handling done there? How?

I'm guessing Account to Trans Date should be to-one... but really not sure at this point.

/-----------------------\          /----------------------\       /------------------\
| Account               |          | Transaction Date     |       |   Event          |
|-----------------------|          |----------------------|       |------------------|
| name                  |          | addDate              |       |  amount          |
| balance               |          |                      |       |                  |
|-----------------------|          |----------------------|       |------------------|
| heldByAcct            | <-\      |                      |       |                  | 
|                       |     \->> | inAcct               |       |                  |
|                       |          | heldByEvent          |<-\    |                  |
\-----------------------/          \----------------------/   \->>| inTrans          |
                                                                  \------------------/

Solution

  • To model a transaction register, I'd suggest making addDate a property of Event and removing Transaction Date entirely.

    It significantly simplifies the work you need to do to manage the object graph, and removes the need to de-duplicate the dates.

    If you need to produce a list of unique dates, you can do it on the fly with distinctUnionOfSets as described in the answer to this question: CoreData get distinct values of Attribute.