|
Hello, This might be super obvious but I'm new to the whole iOS ecosystem and I'm having a bit of trouble figuring something out. I'm creating a simple app that will allow me to create recipes, and with that I need to be able to add ingredients to recipes. I have a View that will allow me to create a new recipe, however I don't know how to initialize that view without already having a recipe to reference. I have tried passing an empty recipe through to the CreateRecipeView and that works until I need to be able to reference the actual CoreData object when trying to add an Ingredient. I'll post some code down below and perhaps I can get pointed in the right direction - I feel as though I may be close but there is just something that I don't understand. HomeView
CreateRecipeView
And then further down...
This will not work because of the empty object that I passed in. However, I don't know what else I can do - it needs to accept a recipe to move forward. I've tried making the Recipe optional and going about it that way but the call to CreateRecipeView still wants a recipe and I don't know what to do. Thanks for any assistance. |
|
For creating a CoreData entity object you must have the managedObjectContext. Otherwise, it doesn't work.
So for example: What you could do is creating the Recipe object in your init method of Recipe. |
|
hi Nathan, to start, i think you'd find it easier to use a sheet to open up a "add new Recipe" view, rather than navigate to it. that allows you to either "save" or "cancel," where:
as for then navigating from an "add new Recipe" to an
where
and you have defined an in-memory
this would mean expanding the "save" operation in
i think i got all the basics above. if you'd like to take a look at some code where i went through this some time ago, take a look at my ShoppingList15 repo on Github, where i use the language of "draft" or "in memory" strcuts/objects quite a bit to collect data first, and only commit those data to Core Data objects once the user says "save." one final idea: you might consider adding ingredients to a recipe withing the hope this helps, DMG |
|
Thank you for the comprehensive suggestion @delawaremathguy! I am going to try this out, it sounds a lot less troublesome than trying to pass contexts around all over the place. |
|
Nathan, you'll still have to pass around a managedObjectContext. you need this because it's your window, or "hook," into Core Data. you need this to be able to create new (and delete existing) objects. if your "main view" provides an array of if you open a sheet for an "add new recipe" view, then you probably want to pass that context into the sheet (sheets don't automatically inherit the environment of the presenting view), and retrieve it. so something like this seems normal for most code out there these days: put the managedObjectContext's into the sheet's environment directly:
and then retrieve that managedObjectContext in the "add new recipe" view, as you already have it in your code:
good luck, and we hope to hear more from you in the future. regards, DMG |
|
Hi @delawaremathguy, Thank you for the tip about passing the MOC thru the wrapper on the sheet! I don't think I would have figured that out myself. I took your advice on using the sheet and creating a DraftIngredient struct to hold temp values and then assigning them to an Ingredient on save and that worked beautifully! I did decide to create ingredients and add them in the same page as recipes as that felt pretty good from a UX perspective and I think it was easier on me to conceptualize and execute. I also did check out your ShoppingList15 repo which helped me with some ideas about what to do in the future layout for my application as their business domains are quite similar. Thank you for all of the patience and help that you have provided! I suspect I will be asking you more questions in the future. :) Nathan |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.