|
Hello, First post here, so go easy! :) I have made it up to Day 61, and CoreData is something I am really keen to learn thoroughly. I have previously developed a prototype of an app using CoreData using UIKit, but I am keen to learn SwiftUI now. I've got the Friend app working fine with structs for the data and have converted the main data into CoreData now. So far the app decodes the JSON, imports it into CoreData, and displays the information in the app using the CoreData Model. So far so good. I'm at the point where I now want to click on a User, and it go through to the User detail screen, to show both the information on the User and the list of friends etc. However, I believe the slight change that takes place during the conversion to CoreData is around pushing the data around the App. I have fetched the data in the ContentView and it contains a FetchRequest variable of all the users. Now when I click on a user, I am struggling to push the data through to the DetailView. Am I even supposed to push the data, or should I be running a new fetch request inside the detail view based on the user selected? Previously I pushed the user through to the detail view, and in the init() of the detail view used the ID's of the friends to create a new array of the users that were the friends of the User. However when I try and push the fetch request through now, I get the following error :
So do I run a new fetch request in the detail view? Or am I missing something? It doesn't feel like the moving of data around in an app using CoreData is really covered in the tutorials up to the challenge so I am struggling a little..... I guess what I'd like help on is the core (pardon the pun) concept of using CoreData in an app with multiple views. Thanks in advance, Chris. |
|
Hi Chris, I think I'm having the same problem you had trying to pass my fetch results using NavigationLink from ContentView to DetailView. Were you able to solve this? Please let me know. |
|
hi Chris, i would start by simply passing a selected user to a DetailView, and let the DetailView then read the friends from the user without another core data fetch. perhaps the display of all users is something like this:
and so the DetailView would be something like this:
there's a little ugliness in the now there are two problems to consider.
you might want to take a look at my ShoppingList14 project, where i struggled for some time to get the right protocol. lots of comments (although i have not reviewed them carefully recently). i also have the additional functionality of providing a DetailView with default parameters when there is no user ... making the DetailView be, effectively, an "add new user" view that can be opened in a sheet (and not through a navigation link). hope that helps, DMG Edit after the fact: i forgot to mention that if you make a change to the friends of a user, it may not necessarily trigger an update for some views where the user is being observed, depending on what you show. if changes to the user's friends don't get updated on screen, be sure to do an explicit |
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.