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 CachedUser
and CachedFriend
Entities in the data model for this project.
Each User
can have many Friend
s, that part is clear.
However, the inverse relationship is a bit trickier to think about. Each Friend
represents (shares a name and id with) one User
in the data. But, each Friend
can also be linked to (can be a friend of) many different Users
in the data.
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?
- A one to one relationship between
User.id
and Friend.id
- A many to many relationship between
User.friends
and Friend.users
(even though Friend.users
doesn't exist as a property in the data)
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 User
has a property named friends
of type [Friend]
. The Friend
struct only has 2 properties, id
and name
. The actual data is being downloaded from this link
struct User: Identifiable, Codable {
struct Friend: Identifiable, Codable {
let id: UUID
let name: String
}
let id: UUID
let isActive: Bool
let name: String
let age: Int
let company: String
let email: String
let address: String
let about: String
let registered: Date
let tags: [String]
let friends: [Friend]
var formattedRegistrationDate: String {
registered.formatted(date: .abbreviated, time: .omitted)
}
}