|
Woe is me! I am completely stuck on this challenge... My data was originally stored in a struct User, with a nested struct Friend. When I created a Core Data database, I created two entities, User and Friend. I gave User an attribute of 'friends' with type of Transformable and gave it a custom class of type [Friend]. I did not use any relationships. I set User and Friend to None/Manual and then created the NS ManagedObject subclasses. After adding Codable to both classes, I ended up with code that looked like this: User:
Friend
Am I on the right track? In my contentView, do I need to load the JSON data into my old struct first, or can I decode it directly into my new Class, like this?
If you completed this Challenge and want to share how you did it, that would be fantastic. |
|
Yeah i tried to parse the JSON straight into Core Data but i couldn't make my Entities conform to Codable based on the NSSet that is created when i create a relationship of one to many. I have been trying to find an answer but i haven't been able to so far. I have just ended up parsing it into the old structs from challenge 60 and then putting into Core Data. A lot of work. I would also like to know if anyone has successfully parsed it straight into Core Data. |
|
Thanks for the reply Newy! I too have moved on from trying to parse the JSON directly into my Core Data entities. No matter what I did, I found that editing my NSManagedObject subclasses for User and Friend to make the codable resulted in the app crashing... So as I move on with the next set of days (on Day 63 today) I'm going to work on doing the challenge as you did... that is, decoding the JSON into my structs and then writing some function to write that data into my Core Data through some managed object context (moc). In my searching for answers, I found two other people who followed this method successfully and are good resources for people struggling: Although, I find it strange that neither of them seem to have tackled the question of how to write in tags, which in our structs is a simple array of String. In my tinkering, I ended up setting the tags attribute in my Core Data .xcdatamodeld file to Transformable, and then in the Data Model Inspector, setting the Custom Class to [String]. Whether or not this will work... I'll report back later. |
|
|
|
Well, I was able to get it working today. There was some strange stuff with Core Data that would cause the app to crash, and once that happened, no amount of CTRL+Z would get it to stop crashing, so I had to scrap several projects and start over. It might've had to to with changing attributes/entities in Core Data after writing to Core Data, but I'll try to figure that out later. The custom Class for the tags attributed worked suprisingly well. It was as simple as assigning the tags attribute with the [String] property from my User Struct.(see code below) In my code, I use .onAppear in ContentView to call fetchJSON, which parses the JSON into my User Struct, then passes that [User] into my saveToCD function.
Maybe there's a better solution to the Friends array, but this is what finally got everything showing up properly. Using Date and UUID for the registered and id attributes, respectively, also posed no problems. For 'friends' data, I just made a many-to-many relationship between my User and Friend entities. Both CDFriend and CDUser have constraints on id. |
|
awesome job. ill give that a try. im working on my own app at the moment but ill be revisiting this to see if it can optimised anymore. Might wait till WWDC to see what changes or optimisations are made to swift ui and core data. |
|
I'll have to try it like this as well... putting it into structs and then converting the structs to Core Data entities. There must be a way to do it directly, but I can't wrap my head around how it's supposed to happen. |
|
I think this is the direct way. |
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.