|
Hello, everybody I've been stuck at this project for a long time now and have already tried everything I can think of but I haven't found a solution. I am able to fetch the User data but the friends array for a user is either empty or it has an inaccurate count. I can't find the issue. Any help would be appreciated. Here's a link to my project on GitHub: https://github.com/CarmenRoxana/UsersAndFriendsCoreData/tree/master/UsersFriendsCoreData This is my function for fetching data: `func fetchData() { guard let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json") else { print("Invalid URL") return }
|
|
Hi Carmen, I'm about to set out on this challenge today and I'll return here to let you know how it goes. Perhaps my experience will help me to help you, once I've had a few hours to wrap my head around the problem. Until then, Russ |
|
Hi Carmen, I'm almost finished Day 61 and I definitely ran into the same thing you have. I'm planning to post a Twitter thread documenting some turning points and key discoveries, but let me post here what I hope will be helpful to your specific question. The code you have works well enough to write to Core Data when only one entity is involved, Users, but as you've found, it gets sticky when trying to populate the Friends entity (and Tags entity if you choose to set that up later). What I learned:
Here's what my extension to
Here is how my entities are set up: Now... on a related note...
Here is what that looked like in practice for me:
Finally, I should note that I'm working with the iOS 14 "full" SwiftUI app lifecycle. Largely taken directly from the sample code in the iOS App Core Data template from Xcode 12, here are the contents of my
That is what allows for the following key line just before decoding directly into Core Data objects – where we get a reference to the managed object context:
Carmen, I hope I'm not oversharing and that this helps. Good luck! |
|
Hi, Russell Thank you very much for the detailed explanations, the links and for sharing your elegant solution. There's quite a bit I have to read and understand, my skills are quite primitive at this point it seems :) I don't know how to debug in XCode yet. Is there any resource you would recommend for me to learn from? Thank you again, Carmen |
|
Hi Carmen, I am certain you know a lot! You've got this. You are more than halfway through this course, after all! 🚀 ☺️ Full disclosure, total honesty – a lot of the time, my debugging approach is just a few strategically placed It seems some people frown on that, but I say – use what works for the situation at hand. For example, this series of output:
...helped me to understand that my code was working asynchronously (while some threads were still actively downloading placeholder user images from a website and saving those to Core Data, another thread in my program was already pulling those images from Core Data to show in a view). I do make use of breakpoints and the Xcode debugger, but I have a lot to learn about what Xcode offers there myself. Here is a 3-minute video I made for my own students to introduce the debugger. (I teach high school computer science classes in Ontario, Canada). Finally, Paul Hudson has a really nice overview of three approaches to debugging. I haven't read all of this, but it looks great! Take care and good luck, Russ |
|
I have been working on this for some hours now. I think the solution Russel posted is nice and clean but at the same time requires a lot more skills than I have at this point of the course. (I tried to follow it but now have more questions than before:D) I'm wondering what Pauls intended approach was on this. I think there must be a way to do this with the skillset on has acquired at this point of the course. Maybe with an intermediary struct where the json gets parsed to and that then is then written to Core Data? |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your 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.