简体   繁体   中英

Adding An Item To An NSSet For A Core Data One To Many Relationship

I have a core data relationship where one entity holds many of another entity. As far as I am aware each instance of the many class is held inside an NSSet? inside the one class. (?)

My question is - what is the best way to add items to this set? I figure this must be a very common problem - but I cannot seem to find an easy method.

This is my attempt: (This is all taken from the one class)

static var timeSlotItems: NSSet? //The Set that holds the many?


...



static func saveTimeSlot(timeSlot: TimeSlot) { //TimeSlot is the many object
    retrieveValues()
    var timeSlotArray = Array(self.timeSlotItems!)
    timeSlotArray.append(timeSlot)
    var setTimeSlotItems = Set(timeSlotArray)
    self.timeSlotItems = setTimeSlotItems // This is the error line

}

Where retrieveValues() just updates all the coreData values in the class. TimeSlot is the many object which I want to add.

I get an error on the last line, the error is: "cannot invoke initializer for type Set<_> with an argument of list of type Array"

Am I conceptually wrong at all? Thanks!

For one-to-many this is easy. Just use the reverse to-one relationship.

timeSlot.item = self

For many-to-many I use this convenience method:

// Support adding to many-to-many relationships

extension NSManagedObject {
    func addObject(value: NSManagedObject, forKey key: String) {
        let items = self.mutableSetValueForKey(key)
        items.addObject(value)
    }

    func removeObject(value: NSManagedObject, forKey key: String) {
        let items = self.mutableSetValueForKey(key)
        items.removeObject(value)
    }
}

which is used like this:

self.addObject(slot, forKey:"timeSlotItems")

You've declared both timeSlotItems and saveTimeSlot: as static, so I'm not sure what your intention is there. I suspect it's not what you need.

In the same way that Core Data automatically runtime-generates optimized accessors for attributes, it also generates accessors for relations.

You don't say what the name of the "one" side of the to-many relation is, but if I assume that it's something like Schedule , where Schedule has a to-many relation to TimeSlot called timeSlotItems , then Core Data will runtime-generate the following accessors for you:

class Schedule: NSManagedObject {
    @NSManaged public var timeSlotItems: Set<TimeSlot>
    @NSManaged public func addTimeSlotItemsObject(value: TimeSlot)
    @NSManaged public func removeTimeSlotItemsObject(value: TimeSlot)
    @NSManaged public func addTimeSlotItems(values: Set<TimeSlot>)
    @NSManaged public func removeTimeSlotItems(values: Set<TimeSlot>)
}

Nowadays it is this easy...

For a to-many item named say "Reply", CoreData knows to add a call "addToReplys".

Hence...

p = one Post. your core data Post items have many core data Reply items.

for jr in yourJson {
    r = convert jr to a core data Reply
    
    p.addToReplys( r )

so it's just

p.addToReplys( r )

Full example

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM