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

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!

2      

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?

2      

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

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!

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.

3      

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

2      

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

2      

Oh crap! I totally missed this:

that also exist in another Trip

Sorry for that!

2      

2      

2      

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?

2      

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.

2      

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

2      

Thank you @bobstern

I will check this out.

2      

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

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.