|
Hi Everyone, I'm working on a project that requires a core data store, that sends to a ForEach list. I ended up consulting with an online mentor, who was pretty good at helping me make sense of most of my issues. Honestly, he's great. However, after we were done chatting the other night, I hadn't bothered to check that my ForEach list would still work. Sadly it does not, although the rest of the app seems to be ok. Right now the app builds without issue and runs properly. I can add an attribute, which is part of an entity, and click save. There are no errors present. However, when I go to see my list, there is nothing there. For more detail, the app works by giving the user several fields to fill out, and that data is then stored in Core Data. I'm pretty confident that the data is stored properly, as it prints out in the log. The log basically presents the several fields, and if there's data, that data is presented there. So for example, the field I'm using to test this is accountCompanyName. If I run the app, and then enter 'Test Account', that 'Test Account' is entered in the log beside accountCompanyName. I think my problem is some where in my list view, although I'm not 100% sure. I will put code below. I'm also pretty sure that it's the important code, but if something is missing, please let me know. I'm hoping this is a simple fix, as I'd rather not spend the money on my mentor if I can avoid it! Fingers crossed! I've tried for several hours and haven't had much luck. Thanks! This is where I initialize a functoin call that accesses the core data persitent store. It also has several other methods that are part of the build.
This is the view that has my forEach list. This is the par that should be displaying data, but isn't. The first stuct, AddAccountContainer, was something I added from the help of my mentor. I'm still new to it, so I'm still learning what it does.
|
|
I should have added that AccountBackEnd is the name of the core data entity in question. thanks!! |
|
Well, you are in luck! Paul posted the 53rd thru 56th days of his updated 100 Days of SwiftUI course. These days are ALL ABOUT CoreData. Maybe you should review these updated videos? Day 53: Bookworm (CoreData and SwiftUI) Additionally, his updates may give you code bits to simplify your managed object context and more. Also, it may also help you to NOT think about ForEach being a list. ForEach is a struct that builds other views, possibly a new view 'for each' record in your CoreData FetchRequest. The ForEach struct is embedded inside a List view. |
|
|
|
Hi @Obelix, Thanks again for the additional info. I'm still working through it all so I appreciate that. Cheers! |
|
I see in your DatabaseViewMain struct you have the code:
Paul is quite clear in videos. @StateObject is where you DEFINE the object. Everywhere else you should be referencing an @ObservedObject. This is where Paul's up-to-date CoreData videos will help you, I think. The architecture is greatly simplified. You define your managed object context in the application's boot code as an @StateObject. Then you inject the moc into your application via the Environment. In any views that need your CoreData objects, you'll create a reference to the managed object context by reaching out to the application's environment. Every view will be using the same core data object. Updates, deletes, fetches....all from the same moc. Additionally, you can clear out some old NSFetchRequest cruft by using @FetchRequest property wrappers. So much easier on your cereberal cortex. Personally, I would not even try to help you unravel your code. Not that I don't want to! But it looks too painful. However, if you refactored your code using Swift's updated CoreData structs and wrappers, I'm in! Good luck! |
|
I'll try that out! I electedt to use funcs for my "RefactoringAccount" class, as I had no idea how to use @FetchRequests from a refactored file - sorry if this doesn't make sense. It got really weird for me so I switched from property wrappers to funcs. |
|
Fixed it! Turns out I had three relationships that should have been optional. Cheers! And thank you again for your help! |
|
Nice! Mark your own answer as solved! Give a few more details and mark your comment as Solved, so other might be helped. |
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.