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

Core Data multiple add, iDine example

Forums > Swift

Still learning Swift and Core Data. Having difficulty inserting/adding multiple items at once.

For example, in the iDine app, what if I wanted a button that would add all items from a specific MenuSection to the order (ie order all breakfast items at once)?

2      

Hi! What exactly is the issue? Code crashes? Items are not available?

2      

I'm not sure how to approach the problem. I tried reading the documentation for managedObject but didn't see anything that would do multiple inserts.

Would this require a ForEach? Do a fetch with a predicate for MenuSection then for each item self.order.add? What would that look like? Seems like there would be an easier and less time consuming way?

Thanks for taking the time to help, and stay healthy!

2      

How is your data model set up?

If you have Item and that has relation to MenuSection then Order could be made of "references" to Items which would be done with the many to many mapping.

After selecting Menu, you could just loop over all the items and connect them to new Order entity if that makes sense.

2      

I was just following the iDine tutorial where MenuSection contains an array of MenuItems. A MenuItem is placed into an Order.

I have a SQL background, so always avoided many to many. I'm struggling to learn a new mindset similar to object graphs vs relational databases. I'll have to read up.

Thanks for the suggestion!

2      

Oh, I have just now realized that you are talking about HWS project. I pretty much ignored the SwiftUI stuff here so that is why it did not connect.

Core Data is pretty intimidating when starting but once you get over the initial hurdle it is pretty awesome solution for persisting data. If you have more questions or need further help ask :-)

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.