UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 61, Core Data, can't save Tags when modeled as a relationship to User

Forums > SwiftUI

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

1      

One more puzzle piece...

I added a FetchRequest for CachedTag:

@FetchRequest(sortDescriptors: []) var cachedTags: FetchedResults<CachedTag>

And displayed the total numbers of Tags in there. This number is indeed increasing when I'm trying to save CachedUser and CachedTags, but I'm still struggeling with connecting CachedUser with CachedTag

1      

After spending another 4 hours on this, I finally have a solution (I'm now somewhere inbetween sad and proud). My unwrapping of the NSSet was incorrect.

Wrong approach

let a: Set<String> = tag as? Set<String> ?? Set<String>()
let b: [String] = a.sorted()
return b

Working approach

Still a bit ugly, but hey -if it works, it can't be wrong-

guard let tag = tag else {
     return [String]()
}

var allTags = [String]()
for a in tag {
    if let b = (a as? CachedTag)?.wrappedTag {
        allTags.append(b)
    }
}
return allTags

This is only needed, if you don't follow Pauls advice to store the tags as a comma separated string.

1      

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.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.