I tried to model the relationship between the CachedUser and the CachedTags as a many to many relationship in the CoreData model.
CachedUser holds the user data and has a relationship to tag, including an Inverse
CachedTags should hold the tags, has a relationship to user, also including an Inverse
There is also a User struct which I use for loading the JSON data
struct User: Codable, Identifiable {
enum CodingKeys: String, CodingKey {
case id
case isActive
case fullName = "name"
case age, company, email, address, about
case registeredSince = "registered"
case tags, friends
}
var id: UUID
var isActive: Bool
var fullName: String
var age: Int
var company: String
var email: String
var address: String
var about: String
var registeredSince: Date
var tags: [String]
var friends: [Friend]
}
When I want to update the Cached User and Tags, I'm using this piece of code:
let newUser = CachedUser(context: moc)
newUser.id = user.id
newUser.isActive = user.isActive
newUser.fullName = user.fullName
newUser.age = user.age
newUser.company = user.company
newUser.email = user.email
newUser.address = user.address
newUser.about = user.about
newUser.registeredSince = user.registeredSince
for tag in user.tags {
let newTag = CachedTag(context: moc)
newTag.wrappedTag = tag
newTag.addToUser(newUser)
newUser.addToTag(newTag)
try? moc.save()
}
Saving new a new user with user attributes works fine, but saving the tags to a user doesn't work at all. I've spend a couple of hours now on this and I'm rather out of ideas.
Tags are coming in with the user (verified that via print(...))
I'm aware that I could use the fallback suggested by Paul, saving the tags as a comma separated string, but as some point I will need to be able to store related data as well, so I told myself do it now.
Any hint in what I'm doing wrong would be highly appreciated.
Thanks in advance,
Heiko