Hi Everyone,
This is a long read, but it comes from my biggest frustration and lack of knowledge in SwiftUI, and how it relates to my own project. It’s the thing about injecting un-initialized objects into views. So something like, “AccountViewMain()”, will need to have a value if there’s a variable in AccountViewMain, that’s not initialized. From what I know, every object needs to be initialized. I think I’m right about that. I’ll add my code below.
For the most part, I can make sense of this. Where I KEEP running into trouble is how this relates to core data, and more specifically some help I received from a code mentor I connected with. The mentor was great, but expensive. Christmas is coming up so I’ve got to be careful with how much I spend.
My project has several views that reference my core data persistent store. My mentor used Combine to make it so whenever a user clicks the add button, a new object is already stored in the persistent store. So that when you go back to the list that presents those different objects (in my case accounts), they all show up, even with nothing in them. If I add text to that object, it shows up. But the problem is that whenever a user just clicks to a new object(account), the app automatically creates it. Not a desired effect.
And I know why this happens. It’s because the button I click on runs this code he made. It’s a view that does not ask for injection, and I think that’s because the view my mentor created runs an initializer. This is all cool, but not entirely helpful.
I have another view that looks just like the one my mentor made, but it doesn’t do any of the adding. So I want mine to appear when the user clicks to add a new object, and then use some of the code my mentor wrote to save the new entry whenever a user clicks the save button. This is the desired effect.
Here’s where I run into issues, even though my core data persistent store gets initialized, when I try to add my own view, it keeps saying, “Missing Parameter for accountBackEnd…” So this is assuming that the view called AccountViewMain, has a line like “@ObservedObject var accountBackEnd: AccountBackEnd. For reference, AccountBackEnd is the name of my core data entity. I’ve set everything else up, so it’s all good, but like I said, I run into issues here.
When I add the appropriate objects between the braces, it seems like it just keeps asking me for more and more. Like I'll fix one, and then the next one down the line asks, until I'm at my @main view. When I add it here, it says something about needing to be initialized. I'm pretty sure there's not supposed to be one there. Right?
I’ve tried adding “*** = AccountBackEnd()” to the end of that line, but naturally that doesn’t work. I think it’s something to do with the “= ***()” meaning that it’s recreating that view. Not referencing it. To add to this, whenever I try to look up how to initialize online, I really don’t get anywhere.
Is there someone who could please help me understand what it is that I should be doing here?
I’ll add what I think is pertinent code below. Please let me know if there is anything I’m missing here as far as code goes.
Thank you all kindly!