UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I build Many-to-Many entity relationships in Core Data?

Forums > SwiftUI

I have been been following the Ulitmate Portfolio videos and in there the example provided has a one-to-many relationship (between the projects and items).

I have search through the internet for SwiftUI specific examples and explanation on how to build many-to-many entity relationships and data linking in Core Data. There are many SwiftUi specific one-to-many examples and information on the internet, but scant few dealing with many-to-many.

To give a concrete example of the data model I am making : two entities - person and group.

A person can belong to 0 (nil) to many groups, and similarly a group may have 0 (nil) to many members.

When entering the details of a person (firstName and lastName) I would also add details of any groups (nil -> many) they belong to. On the other side when adding a group (name) I would also add any members (nil -> many) that it may have.

So I am looking at linking the two entities with a many-to-many relationship.

Is there anyone who could help me understand how to do this in SwiftUI?

2      

you can do many to many relationships in core data you just need to change the relationship type in the pane on the right (I think its called the attribute inspector. The predicates can then get a bit more complex.

what I would tend to do here though is introduce another entity called (groupMembership) which has a many-1 relationship with both your other entities

you can then store core person info (firstName, lastName etc) in person

you store details ofnthe group itself in group

and then you store anything about the membership in the new entity e.g member since, membership fee etc

typically then your queries are done on groupMembership e.g. fetch me all groupMembers where group = xyz
you can then use object dot notation to show the name of the person in your view

thats how I do it anyway. Im sure there are database admins who may say Im doing it a silly way :)

2      

Thank you for the reply @rlong405. I already have the many-to-many set up in the attribute inspector for these entities.

What you have suggested is to convert the many-to-many relationship, into two one-to-many relationships

Person - Group ---> Person - Intermediate - Group

Whilst it would be a way forward, it adds a new entity and complexity I do not need, so would like to avoid, if at all possible. Of course, it may be difficult to avoid.

2      

hi

yes it would be

1) person to intermediate (1 to many) 2) group to intermediate (1 to many)

fair enough if you want to stick with the many to many.

sounds like youve got it setup. Are you facing a particular challenge? Is there a fetch request that you are specifically struggling with?

Richard

2      

If your relationship itself doesn't need or have any additional information you don't need the extra entity. CoreData will handle this for you just fine and you avoid the additional code.

On the other hand, if your relationship has additional information you have to create the extra entity and two 1:m relationships to store the additional information.

Extra information: Even if you choose not to have an extra entity, under the hood CoreData will create the extra entity(=table) but will handle the management silently. You don't have to bother with it and you and will never touch it directly in your code. You can completely rely on the given CoreData API.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.