WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Many To Many Relationship Core Data SwiftUI

Forums > SwiftUI

Hello all, I'm currently in the proccess of building an app that allows you to create an Item with some attributes. Also you can create a Trip with some attributes.

Now what I'm trying to figure out is how to setup a Many To Many relationship in Core Data, so that You can create a new Trip and Add Items to that trip from a main Item Catelog. Each Trip may have mulitple Items that also exist in another Trip.

So I want to be able to select the Items from a list to add to the trip and then save those items under the Trip.

If anyone has any experience with Many to Many relationships in Core Data, your help would be much appreciated!

1      

Are you sure this is many-to-many? How I understand your use case is that one trip can have many items. Although, a different trip could have the same items as the trip before, it's still one to many. Or did I misunderstand you?

1      

I would also think that this is rather a One-to-Many case. Though I'm still running into issues retrieving my items, I used this tutorial to set up core data: https://www.hackingwithswift.com/books/ios-swiftui/one-to-many-relationships-with-core-data-swiftui-and-fetchrequest

This should tell you everything you need to abstract it to your case.

Cheers,

Pascal

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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

Each Trip may have mulitple Items

One trip can be connnected to many items.

that also exist in another Trip

One item can be connected to many trips.

That's a many-to-many relationship.

You would especially need a many-to-many relationship if, say, you wanted to give the user the ability to search a) all items contained in a single trip, but also b) all trips that contain a particular item.

It's really no different than something like Customers and Products (each customer can buy many products, each product can be bought by many customers) or Authors and Books (each author can write many books, each book can have many authors), etc.

If it was a one-to-many relationship, then each item could only occur in one trip but that wouldn't satisfy @Lukester45's criteria.

2      

You right about that, however I want each Item to be able to know which Trips It is in.

So i figure its Many To Many, because;

An Item can belong to multiple Trips

And a Trip can have multiple Items

1      

@roosterboy is right, this is what I am looking for and am having trouble implementing it.

1      

Oh crap! I totally missed this:

that also exist in another Trip

Sorry for that!

1      

1      

1      

Finally getting a hang of this,

So in the first Image you can see I have a Main Catelog, which has all the items.

In the second Image you can see the TripsTabView where if you press plus it will bring up the third image.

The third image is a sheet where you can add the trip name, and select items from the main catelog list.

Now where i am stumpped is once i select the item and hit save, is how do i tell core data about those selected items being under that specifc trip?

1      

Update! Ive now been able to record in a temperary array the selected items from the list of items to add to trip.

Now with this temperary array Im still not sure how to send to core data that I want to add these Items to This Trip.

Because in the end I want to be able to see which items are in a specifc trip, and also be able to see which trips each item is in.

1      

Many-to-Many relationships use a "join table" to define a relationship between two other tables. In this case, there would be a Trips table, an Items table, and a join table (perhaps called ItemsOfTrips) containing one record for each Trip/Item pair.

However, CoreData tries to simplify this by hiding the join table from you. See the section "Many-to-Many Relationships" at about the midpoint of this tutorial: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/HowManagedObjectsarerelated.html

1      

Thank you @bobstern

I will check this out.

1      

Wanted to add this here for anyone looking for more resources on Many To Many relationships.

This video is a super great explination of core data and the relationships between entinties.

https://www.youtube.com/watch?v=huRKU-TAD3g&t=967s

Hope this helps!

Luke

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.