|
In my app, I have a CoreData entity "Work", for music classical pieces, which has an "instruments" related property. It's a Many - ToMany relationship, as one work can bre written for many instruments, as an instrument can be used on many works. I present on one of my app views the list of instruments that work is written for as follows:
And add instruments to this list on another view, summing up the selected ones from CoreData on an Instrument array, with:
Now, my problem is that in some cases I need to include to this Set of Instrument for one Work object more than one repeated Instrument. For example: string quartets have two "Violin", and I don't want to create two "Violin" instruments on its own. I think (I'm not sure) that this behaviour is controlled on my DataController class whith:
And, in fact, is very useful for all of the rest on my app entities to avoid duplicates. But, how can I allow this precise object duplicates, only for this entity and relationship? Thank you very much in advance. |
|
hi Óscar, just for clarification: are you requiring that there be only one instance of a Violin object in your Core Data object graph, yet you'd like allow multiple associations of it to same (BTW: the i have an idea; it just depends where you want to go. DMG |
|
Thanks, DMG. Yes, exactly that: only one Violin, one Cello, one Piano... on my CoreData "Instrument" entity. And correct: I'd like to have repeated "mentions" of the same related "Work" entity: Instruments:
Works:
When I add a second "violin" object, it "merges" automatically, and only one appears, and saves. |
|
You may consider augmenting your model with the instrument's Part, or Chair. For example, a piece will have a part for a Violin I and a Violin II. I think this might model an actual composition, or score. Changing your model to reflect the instrument's Chair (first Chair?) or Part may better represent real-world compositions. |
|
hi Óscar, so we agree on your plan, but to implement it, my suggestion is to use some intermediate indirection. first, when you associate a Work with an Instrument, you don't have any detail regarding this association. so your Core Data model is basically:
where can you store, say, the number of violins (Instruments) associated with this string quartet (Work)? unfortunately, you can't do it in either of the Work or Instrument entities (because these objects have plenty of associations with other objects throughout the Core Data graph). one answer i have used is to place an intermediate object in the relationship, that looks like this
where the single arrow indicates "to one", and the a class pseudo-definition for
(a) how do you associate an
(b) but what if i already have an association of this
(c) how do i find which Instruments are associated with a given Work?
(d) can i get a list of Instruments associated with a given Work and how many times they are used?
and on it goes. i'm not a database-guy, as such, and it takes a little time to wrap your head around what i think is called a join record, but i have used this idea before and it works. in short, whenever you need more data about a relatonship between A and B, such as "why or how exactly is A related to B," you can add such an intermediate record to represent the relationship, as well as store the why or how exactly information. hope that helps, DMG (disclaimer ... |
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.