-1
  • Two Entities
  • Gymnast is one to many to Meet
  • I would like to when I save a new meet, it gets assigned to as a meet to each gymnast where they can then score their individuals scores for each event

Maybe I completely wrong in my logic, but here is what I am trying to do

let request = NSFetchRequest(entityName: "Gymnast")

    do {

        let entities = try AD.managedObjectContext.executeFetchRequest(request) as! [Gymnast]

        for item in entities {

            if let first = item.valueForKey("firstName"), last = item.valueForKey("lastName") {

                print("Name: \(first) \(last)")

                let myMeet = NSEntityDescription.insertNewObjectForEntityForName("Meet", inManagedObjectContext: AD.managedObjectContext) as! Meet

                myMeet.meetName = "Winter Classic"

                let myMeets = item.meets!.mutableCopy() as! NSMutableSet
                myMeets.addObject(myMeet)

                item.meets = myMeets.copy() as? NSSet

                AD.saveContext()

            }

        }

    } catch {

    }
}

}

debratton
  • 51
  • 1
  • 7

1 Answers1

0

I think it is not ideal to replicate the Meet object over and over again for each Gymnast. For example, the meetName will be stored multiple times. I am not sure this is intended.

However, going with your setup, your problem is how you assign the to-many relationship. For a one-to-many it is always easier to simply set the to-one relationship. (Remember, there is always a reverse relationship in the Core Data model.)

Thus,

myMeet.gymnast = item

is all you need.

To add and remove to-many relationships, you can use this extension:

// 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)
    }
}
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks both of you for your responses. I agree and I have changed the relationship to many to many but still am not sure how to save it. – debratton Mar 26 '16 at 18:43
  • 2016-03-26 13:36:48.326 GymScore[1111:343330] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value in to-many relationship: property = "gymnasts"; problem = {( (entity: Meet; id: 0x13f7ac240 ; data: { – debratton Mar 26 '16 at 19:06
  • NSValidationErrorKey: gymnasts, NSLocalizedDescription: The operation couldn’t be completed. (Cocoa error 1550.)] – debratton Mar 29 '16 at 20:24
  • extension Meet { func addObject(value: Meet, forKey key: String) { let items = self.mutableSetValueForKey(key) items.addObject(value) } func removeObject(value: Meet, forKey key: String) { let items = self.mutableSetValueForKey(key) items.removeObject(value) } } – debratton Mar 29 '16 at 20:25
  • if let meetname = meetNameText.text { item.meetName = meetname item.addObject(item, forKey: "meets") – debratton Mar 29 '16 at 20:25