1

My app has Monster objects that can be placed in a team. It is critical that the same species of Monster can appear in a team more than once. Each team contains 6 monsters.

Initially I had a to-many relationship setup between Teams and Monsters. But Swift's Set would not allow for the same type of monster to appear twice in a team. This is an issue as my app requires the ability to have the same monster appear in a team more than once.

I have read it is possible to use transformable or binary types to store arrays in Core Data. Unfortunately most resources were not in Swift and I am unable to convert. Would one of these avenues be the solution I need? If so, should I use transformable or binary? Lastly (and most importantly) how do I go about storing and retrieving an array of custom objects? I have never used NSCoding before and do not know where to start.

3
  • Have you thought about using 3 entities team,monster and species? Commented Sep 11, 2016 at 19:42
  • Wouldn't it end up with the same problem? This time with adding the same species of Monsters more than once on a team. The key is that each team has 6 number of slots, and those slots can be filled with the same creature multiple times. Core Data seems more fitted to handle the scenario where we list all species that appear in that team, but not the members that occupy the slots themselves, since those can be repeats. Commented Sep 11, 2016 at 20:32
  • You should not rely on sets in this situation. Your data model was reasonable before. Since you can have multiple monsters of the same type, you need to distinguish them by another method. Commented Sep 11, 2016 at 22:58

1 Answer 1

1

Rather than using transformable or binary attributes, I would add a third entity: TeamMembership. A TeamMembership object represents a single instance of a Monster being a member of a single Team. So the TeamMembership object will have to-one relationships to Monster and Team.

Each Team will have six members, so it will have a to-many relationship to TeamMembership. Likewise, each Monster will potentially be a member of several Teams (or indeed a member of the same Team several times) so it will likewise have a to-many relationship to TeamMembership.

The relationship diagram will look something like this:

enter image description here

With this setup, a full Team will be related to six separate, distinct TeamMembership objects. Each of those objects will be related to one Monster. But it is entirely possible that two (or even all six) TeamMembership objects are related to the same Monster, which (I hope) fulfils your requirement. The downside is that adding a Monster to a Team involves creating a new TeamMembership object and setting its relationship to the relevant Team and Monster objects, rather than just setting the relationship directly between them.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi pbasdf, I am intrigued by your answer. Thanks for sharing! Can you clarify one thing? If I have a team with two instances of the same monster in it, it would mean my Team has a set of TeamMemberships. In that set, wouldn't two instances of a monster with identical attributes still be reduced to a single instance in the set? Or would CoreData recognize they are different, even though the monsters in the TeamMembership are the same.
The set would contain TeamMemberships, which are different, even though they might themselves be related to the same Monster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.