开发者

How would I model this many-to-many relationship in Core Data?

开发者 https://www.devze.com 2023-03-18 23:01 出处:网络
I have this Core Data relationship: Deck <<-->> Cards <-->> Card Defaults But also: Deck <-->> Card Defaults

I have this Core Data relationship:

Deck <<-->> Cards <-->> Card Defaults

But also: Deck <-->> Card Defaults

How would I model this so that I would be able to create a deck with many cards, each with only ONE different default? I'm n开发者_如何学Pythonot sure how Core Data would be able to associate the fact that for each deck, there would only be one default for each card. Just from the Deck <<-->> Cards <-->> Card Defaults, it seems Core Data would think that each card from a deck may have many defaults.

However, if I do Cards <--> Card Default, then I wouldn't be able to have different card defaults for different decks.

Is there something I'm missing here?

Thanks for your help!


As a rule of thumb when you find yourself creating a situation like:

Deck<<-->>Card Card<-->>Default Deck<-->>Default

... that is an indication that your model logically requires a linking entity between two of the other entities.

I think you need:

Deck<-->>Default<<-->Card

This uses the Default object to relate each Card with a specific Deck but only through the Default appropriate for that Deck.

Don't be afraid to make multi-jump relationship paths. Core Data's ability to handle very long keypaths and complex models is its great strength.


You need a one-to-one between Card and Card Default. You also need a one-to-one between Deck and Card Default. This way, a Card Default is associated with a Deck and a Card.

0

精彩评论

暂无评论...
验证码 换一张
取 消