|
Not sure how many people really struggled with it, but I personally did and I couldn't really find a topic that had a good solution to load the JSON data directly into Core Data, so I thought I'd present my solution and get some feedback :) First of, the entities and attributes. Those should be pretty straigh-forward, the only thing I struggled with was the relationship between User and Friend. Ideally I would've like having just an array of links to other Users and I'm sure it would've been possible to decode it that way, but I'm really trying to do at least one part a day and that was just not possible for me to figure out in the time I gave myself. For the constraints I simply used
Then I set the code generation to manual for both and created the Adding conformance to Identifiable
Both classes needed to conform to Making them conform to Decodable Since there is no use of encoding the data again I'm not gonna go into that here, but it shouldn't be a problem to add it. After adding the protocol I needed to add the initializer, the problem was that I somehow needed to call it's parent initializer and give it a context and entity to use, so how could I provide that context?
by using
So first I created an extension on
now inside the
so all that's left is to add the actual conformance to the classes of our entities, it's the same for both
Hopefully some people will find this when they struggle with the challenge and find this useful. If you have some feedback please feel free to post it here, I'm sure there's stuff I could improve :) |
|
Firstly, thank you, the one headache I have had was the reload of the data causing multiple instances of Friends for each User, saw your commented code for the Secondly, just wanted to add the following things I did to simplify the code (albeit it by 1 line) for the tags array In the User Entity I made the tags (Attribute) Transformable (Type) and added a Custom Class of Array<Any>, this mean in the User+CoreDataProperties.swift file I could utilise the following
In the User+CoreDataClass.swift I could tehn utilise the following:
To access in my view all I need to do was create a wrappedTags variable and use a ForEach |
|
Thanks for the feedback! I didn't look too much into Core Data aside from what I read in the 100 days (or maybe I missed Transformable), definitely good to know. |
|
Really nice way with decoding JSON directly to into CoreData entities, very inspiring. In my solution I made a little change to data model thou - list of friends is just regular array of UUIDs, got from mapped array of Friend struct items. Later on I get it simply with another request with predicate like On the other hand Tags can have dedicated entity, with many to many relationship, as many users have same tags. One thing made me confused too - while calling Best, Kris. |
|
Thank you! I am now starting this challenge and your post is a very helpful and clear summary on CoreData and Codable. This is my first time dealing with data models and entity relationships but I wonder if the challenge data wouldn’t fit a many-to-many data model better. From the JSON it’s clear that a user can have many friends. However, given that on the challenge instructions nothing is said about the uniqueness of friends among different users, I think it is possible that some users share the same friends. In this case, shouldn’t the If this is true, when a shared friend is decoded a second time into your I did a simple test project with this model:
like this
When I used the
And if I flip the two last lines of the Friend assignment order for Mary and Peter I get John with the When I use the |
|
@seriouslyhypersonic YOOO Thank you! I was using the one to many relationship with both id's being constrained and that was actually inhibiting my Friend objects from being saved properly. |
|
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
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.