Home > front end >  Core Data 'to-many' relationship save object in a dictionary not a set
Core Data 'to-many' relationship save object in a dictionary not a set

Time:01-27

I'm working with Core Data and have been using this hackingWithSwift tutorial. I have two entities with a one-to-many relationship.

  1. Country Entity
extension Country {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Country> {
        return NSFetchRequest<Country>(entityName: "Country")
    }

    @NSManaged public var id: UUID?
    @NSManaged public var fullName: String?
    @NSManaged public var shortName: String?
    @NSManaged public var candy: NSSet?
    
    public var wrappedShortName: String {
        shortName ?? "Unknown Country"
    }
    
    public var wrappedFullName: String {
        fullName ?? "Unknown Country"
    }
    
    public var candyArray: [Candy] {
        let set = candy as? Set<Candy> ?? []

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

}

// MARK: Generated accessors for candy
extension Country {
    
    @objc(addCandyObject:)
    @NSManaged public func addToCandy(_ value: Candy)

    @objc(removeCandyObject:)
    @NSManaged public func removeFromCandy(_ value: Candy)

    @objc(addCandy:)
    @NSManaged public func addToCandy(_ values: NSSet)

    @objc(removeCandy:)
    @NSManaged public func removeFromCandy(_ values: NSSet)
}
  1. Candy Entity
extension Candy {
    
    @nonobjc public class func fetchRequest() -> NSFetchRequest<Candy> {
        return NSFetchRequest<Candy>(entityName: "Candy")
    }
    
    @NSManaged public var id: UUID?
    @NSManaged public var name: String?
    @NSManaged public var origin: Country?
    
    public var wrappedName: String {
        name ?? "Unknown Candy"
    }
}

So Country can have many Candy.

What I'm wondering is if it is possible that when I create a Candy and save it that rather than being added to the NSSet of Candy's in the Country entity is it possible to save it to a NSDictionary or any other collection type besides NSSet? Not sure if I just change

    @objc(addCandy:)
    @NSManaged public func addToCandy(_ values: NSSet) //Change to NSDictionary

    @objc(removeCandy:)
    @NSManaged public func removeFromCandy(_ values: NSSet) //Change to NSDictionary

If this works, cause I would like the Entity id property to be the key and the object to be the value. Not sure if this is possible.

This is how they are being added

                    let candy3 = Candy(context: viewContext)
                    candy3.name = "Twix"
                    candy3.orgin = Country(context: viewContext)
                    candy3.orgin?.shortName = "UK"
                    candy3.orgin?.fullName = "United Kingdom"

                    let candy4 = Candy(context: viewContext)
                    candy4.name = "Toblerone"
                    candy4.orgin = Country(context: viewContext)
                    candy4.orgin?.shortName = "CH"
                    candy4.orgin?.fullName = "Switzerland"

                    try? viewContext.save()

Please ask any questions if this doesn't make sense.

CodePudding user response:

You can use Transformable to encode and decode a Dictionary but you can't change a relationship to a dictionary

  • Related