|
I'm trying to work through the day 61 challenge of adding CoreData functionality to the previous day's challenge of building the FriendFace app. I'm just a bit confused about how to set up the relationship between the Each However, the inverse relationship is a bit trickier to think about. Each Is this a many to many, many to one, or one to many type of relationship? Or, are there two separate relationships between these entities happening here?
I'm not sure. For clarity, for people who may not have looked at this project in a while, this is the way that the structs were set up before trying to add CoreData to the project. So, the
|
|
I think the reason why this is so difficult to wrap my ahead around is just that this data model doesn't make any sense to me. Why would we need to have two separate entities when one of the entities is just a copy of two of the properties from the other entity? |
|
Well, I ended up getting the project to work anyway. But I'm still not 100% sure if I set up the relationship the correct way. If anyone is interested in how I did it, I'll tell you. I created an Entity called CachedUser with properties for id, isActive, age, company, name, email, address, about, registered, and tags. Then I created a "to many" relationship to "CachedFriend" on the entity and named it friends. I also created an Entity called CachedFriend with properties for id and name. Then I gave it a "to many" relationship to "CachedUser" named it friendOf. This seems to work, but It just seems strange to me to have to add a relationship on the CachedFriend entity with a name that doesn't exist in the data, and that is never used anywhere in my code. So, maybe there is a more correct way to do it. But, I guess I'll just leave it this way for now. |
|
You are quite right that in designing a relational database, it does not make sense to duplicate the The original non Core Data design would be more efficient by eliminating the To implement the corresponding efficient design in Core Data, with only a single entity called Incidentally, in Core Data you normally don’t need an |
|
|
|
You don't exactly store the friends array as an array in the data model. But you create a relationship between your two existing entities, and name it friends. When you create a relationship between two entities in your data model, the relationship basically gets created as a new table in your database, which can hold data about many instances of an entity in one place. So, the relationship itself acts as the array. The instructions in my previous comment describe how I created the relationship. But @bobstern provided a solution for how to solve this problem with a more efficient and less redundant data model. |
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.