|
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 “= xxx()” 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! The view that displays my list:
The view that my mentor created:
The View that I’d like to use:
|
|
Use Pass the object into another So, in very skeletal outline:
|
|
Ok I think that might help. Will that stop the compiler from asking about the “Missing Parameter for accountBackEnd…” issue that I've been running into? Thank you! |
|
Ok - So I just did the following: In AddAccountView I added, "@StateObject var accountBackEnd = AccountBackEnd()", to replace "@ObservedObject var accountBackEnd: AccountBackEnd." Right now it just crashed with:
Should I have added that to the view that the user first clicks on though? |
|
OK, but why did you do that? You need to understand why things are the way they are (or should be) before just making changes. What is the flow of your data? Why do you have an It's kind of hard to advise you what to do since none of us know the architecture and guts of your app. I will say that this all (meaning not just this question but all the others you have asked around your use of Core Data) seems like it is way overcomplicated. Core Data can be tricky but it's really not all that hard to set up and use. To use one example: I bring these things up not to discourage you or to berate you for doing things wrong or not understanding stuff. I bring these things up because if you don't really understand what you are doing in your code, you may fix this issue or that issue as they occur, but your code will be a ticking time bomb, just waiting for something else to go wrong and you won't know how to approach fixing that issue because you don't have a good grasp on what's been done before. I would suggest rewatching some of Paul's videos on Core Data and comparing to your own code to see what is different and why any deviations you may have are the right/wrong thing for your particular situation. Other good sources of Core Data info are YouTube videos from azamsharp and CodeWithChris. Donny Wals has some good posts about Core Data at his blog and has written one of the best recent books on it, Practical Core Data (which happens to be on sale for the rest of this week!). A poster from these forums, @delawaremathguy, has a github repo demostrating Core Data with SwiftUI. All of these would be great resources for you to figure out what's going on in your code. And, of course, you can always ask more questions, but be aware that it can sometimes be hard to give good answers since we don't get the "big picture" of your codebase. Good luck! |
|
Hi RoosterBoy, Thanks for writing back. Like I've said befor, I really do appreciate your responses. To answer your first question, I did that as I was under the impression that that view (AccountBackEnd) was first being created in DataBaseViewMain. Or at least first being referenced there. I interpreted your line, "Use @StateObject in the View where the object is created/owned.' to mean that. I gather my interpretation is incorrect. For what it's worth I do follow a lot of videos and read a lot of articles, but clearly I have blindspots that I'm not picking up on or being exposed to. For example, a lot of times I'm not sure how to ask what it is I'm trying to figure out. Like what is my issue even called, where the compiler asks, "Missing argument for.." Honestly, I'm really trying here. Thanks again. |
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.
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.