NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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)?


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


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!


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.


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!


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 :-)


Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

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.