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

Day 61 - Codable NSManagedObjects

Forums > 100 Days of SwiftUI

My day 60 challenge app works grea, but I'm having trouble rebuilding my Friends Struct and User Struct from that project, as Core Data Enities for the Day 61. I have a few problems:

  1. How do I create an [String] for user.tags in xcdatamodel? THere is no array option in the drop down and I can't find it in the lesson's anyware.
  2. Do I make Friend and User both have a one to many relationship, as multiple users will have the same friends, and each user has multiple Friends?
  3. To make the NSManagedObjects Codable, I've created subclasses for Friends and User and then used a coding key to add Codable conformance, but I run in to trouble with NSSet for Freinds. So I'm converting a [User ] to NSSet on the decode and a NSSet to [User on the encode]. Is this what I should be doing here?

Am I approaching this right or am I making more work for myself and there is an easier answer to all this.

3      

Did you ever get an answer to your questions?

I am having very similar problems to you on this one.

3      

Check this link out. It is from Paul's 100 Days of Swiftui Project 12. It will answer your questions - https://www.hackingwithswift.com/books/ios-swiftui/one-to-many-relationships-with-core-data-swiftui-and-fetchrequest

3      

Thanks Newy11527.

Would you mind sharing how you completed this task? I think there is still a lot left unspecified in that particular article you linked to. Such as @nickschelle 's question about how to deal with an array.

3      

I ran into the same issues when doing the challenge. When trying to parse the JSON straight into Core Data i had the issue with dealing with an NSSet and trying to get the NSManagedObject to conform with Codable. Everything else i could except for the NSSet. I couldn't find any solution for it. What i ended up doing was creating my own custom NSManaged Objects through CoreData. I parsed the JSON first into structs and then looped through my array of parsed info and created NSManagedObject for each item then saved it. This is obviously not efficient but its all i could do for now.

As for dealing with NSSets in CoreData you just make the entity Manual, create NSManagedObject subclass files then go to the properties file and change the NSSet into an array so you can deal with it as an array in code.

public var friendArray: [Friend] {
        let set = friends as? Set<Friend> ?? []

        return set.sorted {
            $0.wrappedID < $1.wrappedID
        }
    }

Im not sure if this helps but the best that i could do for now. Its going to keep annoying me too. There has to be a way to be able to do it better.

3      

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.