NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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 Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

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

Not logged in

Log in
 

Link copied to your pasteboard.